0
//SCRIPT 1
Class ColorChanger{
    Public string currentColor;
}

//SCRIPT 2
Class Player{
    private void OnTriggerEnter2D(Collider2D col)
    {
        If (col.tag != currentColor)
        {
            Debug.Log("GAME OVER");
        }
    }
}

If I write SCRIPT 2 part in SCRIPT 1 it will work, but I want to know how to use "currentColor" variable with other scripts?

Daahrien
  • 10,190
  • 6
  • 39
  • 71
  • I don't have the documention in front of me, But you can look it up, but its called something like GetComponent that you use on the game object to convert it to your type and then you can access public variables – johnny 5 Nov 30 '17 at 22:12
  • Thanks johnny found it!!! You too shaman – Domagoj Buljan Nov 30 '17 at 22:34

1 Answers1

1

It all depends if the first script is attached to a GameObject or not.

1.- If it is:

objectwith1stscript.GetComponent<ColorChanger>().currentColor

2.- If is not, then you may want the currentColor to be static:

public static string currentColor;

And then access it via:

ColorChanger.currentColor

3.- if you don't want it to be static for x or y reason (and also no attached to a GameObject), you will need to create an instance of the class, like this:

ColorChanger mycolorchanger = new ColorChanger();

and then use it like this:

mycolorchanger.currentColor

If you use 2 or 3 options the first script shouldn't inherit from MonoBehaviour.

Daahrien
  • 10,190
  • 6
  • 39
  • 71