- Home /
Assign a script to a variable?
http://answers.unity3d.com/questions/209050/assign-a-script-to-a-variable.html
I found this answer on the subject, but it would appear I'm doing wrong here.
In the scene I have an object called _LevelMaster with a script named "LevelMaster" attached too it.
I want my enemy's to grab that script and set it to a variable so they are not constantly using .GetComponent.
in their script I have;
private var levelMasterScript : LevelMaster;
It doesn't spit out any errors, but when it's time to grab from that script, something like;
levelMasterScript.enemyCount -=1;
I get NullReferenceException...
What am I doing wrong?
Answer by Rick74 · Mar 14, 2014 at 11:43 AM
Ok I solved it.
I had to also assigned that variable in Start (). So I needed to add a line;
levelMasterScript = getLevelMaster.GetComponent(LevelMaster);
Seems to work fine now!
referenced from this answer;
http://answers.unity3d.com/questions/494131/unity3d-javascript-reference-script-variable.html
Your answer
![](https://koobas.hobune.stream/wayback/20220613140224im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Javascript 1 Answer
Javascript not being updated, variables being overridden, but C# is fine 1 Answer
Accessing Script From Other Script Causes Lag? 1 Answer
Accessing A Variable From Another Script 4 Answers
Prefabs and variables 1 Answer