![]() |
Help NullReferenceException in ObiRopeCursor (build) - Printable Version +- Obi Official Forum (https://obi.virtualmethodstudio.com/forum) +-- Forum: Obi Users Category (https://obi.virtualmethodstudio.com/forum/forum-1.html) +--- Forum: Obi Rope (https://obi.virtualmethodstudio.com/forum/forum-4.html) +--- Thread: Help NullReferenceException in ObiRopeCursor (build) (/thread-3866.html) |
NullReferenceException in ObiRopeCursor (build) - natko1412 - 11-05-2023 Hi, I am changing rope length in OnBlueprintLoaded callback. This works well in Editor, but in build I get NullReferenceException in ObiRopeCursor script, line 131 Code: NullReferenceException: Object reference not set to an instance of an object I guess that the rope reference in cursor is somehow null. How can this happen if it worked fine in Editor? Thanks in advance for help RE: NullReferenceException in ObiRopeCursor (build) - josemendez - 11-05-2023 Hi! Cursors initialize their reference to the rope component in OnEnable(). Actors -such as ropes- are initialized in OnEnable() too, and get their OnBlueprintLoaded called. Since the order in which Unity calls events for different components is undefined by default (the specific order might change between editor/runtime, and even between different runs of the same scene) chances are the cursor is attempting to change the length of the rope before its own OnEnable() event has been called, so the reference to the rope is still null. A solution is to change the length of the rope in Start, instead of OnBlueprintLoaded. Start() is guaranteed to be called after OnEnable(). let me know if you need further help, kind regards RE: NullReferenceException in ObiRopeCursor (build) - natko1412 - 15-05-2023 Thanks for your reply! I tried moving that part of code to the Start method. Now I get NullReferenceException in this line in my code (in Editor): Code: int solverIndex = actor.solverIndices[i]; In debugger it shows that actor is good, but solverIndices is null. Seems like actor is not initialized all the way when I try to work with it inside my Start() method. This is the whole codeblock I use - it loads positions and velocities from json file and should set them to the rope: Code: var positions = reader.Read<List<Vector3>>("tether_positions"); Actually, while debugging - drom actor I see that solver's initialized flag is false RE: NullReferenceException in ObiRopeCursor (build) - josemendez - 16-05-2023 Could you share the entire script you're using? (15-05-2023, 01:20 PM)natko1412 Wrote: In debugger it shows that actor is good, but solverIndices is null. Seems like actor is not initialized all the way when I try to work with it inside my Start() method. Actors are initialized in OnEnable(), and that's called before Start() so the actor should be completely initialized by that point. kind regards, RE: NullReferenceException in ObiRopeCursor (build) - natko1412 - 16-05-2023 (16-05-2023, 08:40 AM)josemendez Wrote: Could you share the entire script you're using? Yes, here is the script Code: using System.Collections.Generic; I found the culprit! I am using the Mirror networking package and it is enabling networked objects just after the complete scene load. Since my rope is treated by Mirror as networked object, it was actually executing OnEnable and other methods a bit later than the normal offline gameObjects would. So, my settings manager would initialize before the rope. Thank you for your help, it went over my head that Mirror could be messing with execution order. |