Hi everybody,
I know many ppl already asked this, but these threads are usually specified on their problem. Anyways,…
I have a publi bool in one script, and I only want to ask an if-statement with this bool in another script,
how do I do this?
Hi everybody,
I know many ppl already asked this, but these threads are usually specified on their problem. Anyways,…
I have a publi bool in one script, and I only want to ask an if-statement with this bool in another script,
how do I do this?
To access any public information from another script, you must use class inheritance in unity using GetComponent. Check out my tutorial link in my signature and watch video 8 - GetComponent.
There are two other ways to do this.
Can someone give me an example code please?
@SubZeroGaming_1
No offense, but the audio quality of the video is kinda fuzzy
EDIT: It’s C#, BTW
Just write the following line at the begining of the second script:
public YourScript script;
YourScript = instead of writing it, write the name of the script that you want to access in other script, then, if you want to use the variables, write:
script.variableName
if-statement:
if(script.variableName==true/false)
{
}
forgot to mention, after you wrote the first line, go to the inspector of the new script and attach the script there.
Hmm, so like this?
public class Script2 : MonoBehaviour {
Script2 = Script1;
if (Script1.var < 0) {
I don’t quite get it, probably the “Script2 = Script1;” part is wrong.
Help me out please.
Thanks ia
Try this instead:
public class Script2 : MonoBehaviour
{
public Script1 myScript; // then assign this on the Inspector
if (myScript.myBoolean == true) // myBoolean is the bool you wish to compare
{
// your code here
}
}
OK, I’m very close to it.
Now MonoDevelop at least doesn’t mark anything red anymore, and I have a slot in the inspector where I can drag in the script. But for some reason I cannot do it, neither drag or drop nor via “Select Control”.
public Control MyScript;
(“Control” is the name of my other script)
PS: Oh, and BTW, you posted your last post twice
Thanks for the reminder, I hadn’t noticed it.
When you try assigning your script in the Inspector, does it show under the Scene tab? If not, you’d need to add it to a GameObject in the scene. If it is, then could you post the script you’re working on?
If you watched the video, and it doesn’t matter if it’s javascript or C# that you’re working… Stop spoon feeding him, people…
Create a private handle…
private CLASSNAME _referenceName;
Now you need to assign that handle to the component you want to access…First find the gameObject, then assign the component. Unity sees everything as a component.
void Start(){
_referenceName = GameObject.Find(“name of object”).GetComponent();
}
now you can type:
_referenceName.variable.
no, it’s not correct… it’s should be like that:
(write it in the second script)
public XXX script;
XXX = name of the script that you want to access
then go to the inspector of the new script, and attach and original script in there.
void Start () {
SpriteAnimation = GameObject.Find(“Player”).GetComponent();
}
Both scripts are on the same gameObject.
“SpriteAnimation” = name of the current script
“Control” = name of the script I want to access.
The Console gives me: “The left-handed side of an assignment must be a variable, a property or an indexer”
Also, my “inspector” doesn’t seem to refresh anymore, since I deleted the old variable but it’s still there.
I tested by just setting up a quick public bool, but it is not shown in the inspector.
Watch my youtube video…
And actually watch it. It tells you that if you’re on the same gameObject as the script, you can just do GetComponent.
If you’re on the Control script… and you need access to the sprite animation script…
public class Control : MonoBehavior
{
private SpriteAnimation _anim;
void Start()
{
_anim = GetComponent();
}
}
void Update()
{
if (_anim.somebool == true){
}
}