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…
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;
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.
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.