- Home /
Can't reference boolean in another script
I am trying to reference a boolean in another script attached to a different game object. But I keep on getting the error "It is not possible to invoke an expression of type 'boolean'".
The referencing script:
function Update () {
var otherScript: OtherScript = GetComponent(OtherScript);
otherScript.DoSomething() = true;
}
The referenced script:
private var DoSomething : boolean;
function Update(){
if(DoSomething) {
transform.Rotate(0,0,60 * Time.deltaTime);
}
}
This a simplified script I put together, the script I'm really trying to figure out is a lot more complicated but the basic issue remains.. I've been trying to tackle this ridiculously simple problem for a week now, I'm still very new to programming..
Answer by Eric5h5 · Aug 05, 2011 at 06:40 PM
You can't assign a value to a function. But DoSomething is a variable, not a function, so just refer to it like other variables.
Answer by Meltdown · Aug 05, 2011 at 06:50 PM
DOSomething is a variable, not a function.
So you would reference it as
otherScript.DoSomething = true;
You may also want to not mark the variable as private.
So change the variable declaration to :
var DoSomething : boolean;
Answer by SisterKy · Aug 05, 2011 at 06:52 PM
As Eric says. This is how it has to look:
The referencing script:
function Start () { var otherScript: OtherScript = gameObject.Find("TheOtherObject").GetComponent(OtherScript); //you tried to GetComponent without telling where to look, so 'otherScript' was 'null' } // Note: Find() and GetComponent() are expensive. // And the path to OtherScript won't change during the scene. // So we don't need to find it anew every frame. // So we do the referencing in Start() instead of Update(),
function Update () {
otherScript.doSomething = true; // unity didn't realize that 'otherScript' being 'null' would be a problem yet, // because first it had to worry about the (). // It tried to find a function called 'doSomething()' in the referenced Script, // instead of a variable 'doSomething' }
The referenced script:
var doSomething : boolean; // the lower-case isn't mandatory to work but it's good habit
function Update(){
if(doSomething) { transform.Rotate(0,0,60 * Time.deltaTime);
}
}
Here, have a look at this: http://unityscript.com/
bare with the 'for super-dummies'-approach. There's some very valuable information there =)
Edit: Meltdown was faster...
Ooops... missed the 'private' Meltdown has pointed out. now fixed.
Greetz, Ky.
@Sister$$anonymous$$y oh master scripter and tag editor :) won't compile in your script lol :):)))
Let me repeat that... you have html tags in your script lol
Thanks for all the advice! :) it's still broken though. I tried it out and now I get the error " :NullReferenceException: Object reference not set to an instance of an object ".
I tried to reference the object with a tag using:
var taggedObject = GameObject.FindGameObjectsWithTag ("object");
function Update () { var otherScript: OtherScript = GetComponent(OtherScript); taggedObject.otherScript.doSomething = true; // the () here were the problem }
but the same error persists. I also get :ArgumentException: FindGameObjectsWithTag can only be called from the main thread.: now.
(do you see my post formatted properly?)
no, no, that won't work... but now that you mention it, I should have spotted this one. The problem is
var otherScript: OtherScript = GetComponent(OtherScript);
Unity doesn't know where to look for that script, so it assigns 'otherScript' 'null'. Then in the next line you try to access otherScript but it can't do that so that line gives you the nullreferenceexception. try:
var otherScript: OtherScript = gameObject.Find("NameOfTheObject").GetComponent(OtherScript);
(drat! for some reason I don't get e-mail-notifications for this thread =/ )
Greetz, $$anonymous$$y.
@Confused: you can't run code outside functions, you can only declare variables. "Doing stuff" like FindGameObjectsWithTag can only be done in functions.
Your answer
![](https://koobas.hobune.stream/wayback/20220612171314im_/https://answers.unity.com/themes/thub/images/avi.jpg)