- Home /
how do you access booleans from another script
c#
public static bool isActive;
void Start(){ isActive = true; }
void Update(){ if(isActive = true){ some scripting... isActive = false; } }
I need to access is Active from another script, but I'm not sure how to do it. When I use gameObject.GetComponent<>(); it says "Static member "laserScript.isActive" cannot be accessed with an instance reference, qualify it with a name type instead"
any idea how to fix this? How should I use GetComponent?
Answer by GeorgeRigato · Dec 18, 2012 at 11:33 PM
Tried to remove the static already?
The basics in this are:
in original script you do:
public class MyDataClass : MonoBehaviour{
public bool data1 = false;
//Or you can set it to private and create a public method to "get" its value
private bool data2 = true;
public bool getData2() {
return data2;
}
}
In the other script:
public class blabla : MonoBehaviour{
MyDataClass dataInstance;
void Start { dataInstance = GameObject.FindGameObjectWithTag("sometag").GetComponent(); }
void doSomething() { dataInstance.data1 = true;
if(dataInstance.getData2())
//Do Something
}
}
Good Luck!
I'll have to look into it. Unfortunately, for my script to update the boolean it needs to be set as static, I have no idea why but it didn't work without it.
Your answer
Follow this Question
Related Questions
Methods of Accessing scripts - performance & neatness 1 Answer
Can't access a javascript static variable from c# script 1 Answer
GetComponent C# 1 Answer
Can't Access Prefab Script from FPS Controller 0 Answers
An object reference is required to accsess a non-static member 'Lives.loselife()' 1 Answer