-2

For example I have 2 scripts:

I have a boolean in script1 called "boolean" which is set to true

Now I have a second script and want to change the value of "boolean" there. But I have no Idea how.

//script1
bool boolean = true;
derHugo
  • 83,094
  • 9
  • 75
  • 115
  • 2
    Does this answer your question? [How do I pass variable from one script to another C# Unity 2D?](https://stackoverflow.com/questions/22927521/how-do-i-pass-variable-from-one-script-to-another-c-sharp-unity-2d) – obywan Oct 05 '20 at 12:29

1 Answers1

0

You must be a beginner programmer. That is fine. There are dozens of ways and patterns for doing that. Here is one solution.

public class MyScript1 : MonoBehaviour {
  public bool myBool = false;
}

public class MyScript2 : MonoBehaviour {
  public MyScript1 myscript1;

  public void Start() {
    myscript1.myBool = true; // assignment
  }
}

You need to set myscript1 field of your MyScript2 component. It can be done in Unity Editor's inspector window for MyScript2.

hk1ll3r
  • 811
  • 1
  • 6
  • 14