- Home /
Issues using GetComponent
I'm have a boolean variable called 'shieldOn'. I'm trying to access a function that sets the boolean to false using GetComponent from another script and for the life of me I can't.
I get this error
variable. Move it to the line after without a variable declaration. I pretty much copied the example right from the api reference to GetComponent and have no clue why it's doing this. Here's the link: [API: GameObject.GetComponent][1] Here's my code for scriptShield function Update () { if(shieldStrength <= 0) { Destroy(gameObject); //The line below works fine but I want to access the function instead // scriptPlayer.shieldOn = false;UnityException: You are not allowed to call this function when declaring a
//Line below is almost identical example that is given by unity var other : scriptPlayer = gameObject.GetComponent(scriptPlayer); other.SetShieldFalse(); }
} Here's the function in for the shield in the script 'scriptPlayer' static var shieldOn : boolean = false;
function SetShieldFalse() { shieldOn = false; } It works perfectly if I set the variable 'shieldOn' to static and access it from the scriptShield but I want to learn how to use the GetComponent function properlly because I'll need it when I want to access other functions that control non static variables... Help is greatly appreciated. [1]: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html
Answer by getyour411 · Apr 23, 2014 at 02:18 AM
Don't forget the parens after the GetComponent
http://unity3d.com/learn/tutorials/modules/beginner/scripting/getcomponent
Search UA/Google for many other examples, tuts/vids, code snippets, etc on using GetComponent, it's the most frequently asked question.
ok, well I tried looking up some tuts but a lot are out dated... regardless I did try and follow the link you gave me and used the java script provided in it.
I'm now getting this error code:
NullReferenceException: Object reference not set to an instance of an object scriptShield.Update ()
These are the changes I have made.
#pragma strict
//Shield script
var shieldStrength : int = 3;
var playerScript : scriptPlayer;
function Update ()
{
if(shieldStrength <= 0)
{
Destroy(gameObject);
playerScript = GetComponent(scriptPlayer);
playerScript.SetShieldFalse();
}
}
at the line with "playerScript.SetShieldFalse();" is where I get this error... Do I have to create a new empty gameobject then set it to a prefabe and drag it onto the variable in the inspector?
Can you confirm on your player you have a script attached named scriptPlayer? This script has a (public) field defined as playerScript, which means in the Inspector for the GameObject this script is attached to there will be a field for that. Drop and drop your player into that field which will affix the references.
Remove line 15 as you do not need to call GetComponent since you used the drag/drop method. If ins$$anonymous$$d you wanted to learn GetComponent and not use the drag/drop method, I think you are still missing parens around 15 at the end, but I don't work in JS so the syntax might be different.