I have two C# scripts on an object, playerScript and playerSystem. I want to access playerScript from playerSystem. However, I’m completely unable to gain a reference to the second script. All of these attempts return ‘null’
playerScript script_play;
script_play = GetComponent<playerScript>();
script_play = gameObject.GetComponent<playerScript>();
script_play = GetComponent("playerScript") as playerScript;
script_play = gameObject.GetComponent("playerScript") as playerScript;
I’ve tried in Start() and my custom called method. What am I missing?!
Yep. I have been able to make the script object public and attach it through the Inspector and access it from that okay, but I want the future functionality of adding scripts dynamically (AddComponent) and accessing them, plus I also want to actually understand how to use Unity properly
Hi, Have you tried doing the opposite? Can you access the playerSystem script from within the PlayerScript Start() function? If so, the problem is most probably to do with the script execution order.
You might be right there, although my script ordering seems backwards. With two scripts drawing to GUI, the one lowest on the execution order is happening before the one above it and its graphics being overdrawn.
Yeah, that’s what I’m talking about. I understand that the GUI cals are performed in order of the script executions. As such, the OnGUI of one script drawing to a part of the screen will be overwritten by the OnGui of the next script drawing to the same part. I haven’t used any z positioning in my GUI.something calls so I don’t see how Unity could know to draw one set of graphics in front of another (or rather, draw some graphics on the UI and then draw something behind them).