I cant call my variable from other script! Say CS0120 error.

Hi, i need help with my code. I try create globalvariable for my gun animation (its other object then character what i controling)

public class SniperAnimControl : MonoBehaviour 
{

   public bool shooting = false;
   Animator anim;

   // Use this for initialization
   void Start () {
	anim = GetComponent<Animator> ();
   }

   // Update is called once per frame
   void Update () {
	if (Move.shoot == true){
		anim.SetBool("Shoot", true);
	}
	if (Move.shoot == false) {
		anim.SetBool ("Shoot", false);
	}
  }
}

shoot is public bool variable in my other script named “Move”.

I´m sorry my bad english but I´m from czech republic. I read about ten ask and answers on this topic by i didnt find anything useful :confused: Thanks so much that will be propably stupid mistake but i cant find it :slight_smile:

This is all very basic programming and Unity stuff. You would’ve found the answer easily with Google or Answers search

Unless move is declared a static variable, you can’t reference is through the Class name.
You have to reference it through the object instance where the variable is located.

Imagine that you had put the Move script on many GameObjects. If you write Move.shoot, how would the compiler know which of those objects you want to check shoot on ?

You have to get the Move instance from the GameObject you want to check first
If the script is on the same gameobject as SniperAnimControl you can do

Move myMover;
Start()
{
    myMover  = getComponent<Move>();
}
...
if (myMover.shoot) {...

If Move is on another GameObject, you have to find it first

Move myMover;
 Start()
 {
    GameObject moveObject = GameObject.Find("NameOfGameObject");
     myMover  = moveObject.getComponent<Move>();
 }

Thanks, i didnt declare Move.shoot as static variable so it was the fail. :slight_smile: