- Home /
Assigning a script name in the inspector.
I have a script that enables and disables other scripts attached to the game object during runtime. I want to assign the script name for each object individually, in the inspector, but when I do, it throws me a NullReferenceException. I am using the following code:
var aiScriptName : String;
function FixedUpdate () { if(currentSector == sectorNum) { GetComponent(aiScriptName).enabled = true; } else { GetComponent(aiScriptName).enabled = false; } }
Answer by Eric5h5 · Apr 30, 2010 at 03:20 AM
You might not be typing the name of the script correctly, because it will work fine if the name is correct and a script of that name is actually attached to the object. On a side note, generally you should use Update for code like this and leave FixedUpdate for physics only. Also, depending on what the sectorNum check is for, it might be more efficient to set enabled to true or false when the condition actually changes, instead of checking every frame.