How to update a property of Behaviour script?

I have a problem below!

I have a MarkBehaviour class that contain a property called markCount . Depend on markCount property, I will select the sprite on Update() function. At begin, I set markCount property to 0.

But when I call the setMark() function from other behaviour class, I have logged the markCount property in 2 function below, but the markCount property on Update() function was not changed, it still = 0 as the first time I set. It was changed only in setMark() function.

    public class MarkBehaviour : MonoBehaviour {
            public int markCount ;
            void Start(){
                   markCount = 0;
            }
            void Update () {	
		        Debug.Log ("mark manual setted is "+markCount);
		        int cal = calculate (markCount);
		        gameObject.GetComponent<SpriteRenderer>().sprite =numberSpriteArray[cal];

	        }

	        public void setMark(int mark){
		        Debug.Log ("manual set mark from other class, set "+markCount );
		        markCount = mark;
	        }
    }

How can I changed it, please give a solution. Thank you very much!

EDIT

Code of other class that call setMark() function

public class GameoverBehaviour : MonoBehaviour {
	
	public GameObject mark;
	// Use this for initialization
	void Start () {
		
		int markCount = getMark ();

		Instantiate (mark, new Vector3(2.3f,-0.5f,0), gameObject.transform.localRotation);
		MarkBehaviour mBS = (MarkBehaviour) mark.GetComponent<MarkBehaviour>();
		mBS.setMark (markCount);
	}
	
	// Update is called once per frame
	void Update () {
	
	}
   // for example set it to 30
    int getMark(){
       return 30;
    }
}

markCount = 0; in Start() is happening AFTER you call mBS.setMark (markCount); Start() isn’t getting called as soon as you think it is. I’ve run into this problem as well on a few occasions.

Get rid of markCount = 0 (you can initialize the actual variable to 0 if you like).

Problem

When you run this line of code:

MarkBehaviour mBS = (MarkBehaviour) mark.GetComponent<MarkBehaviour>();

You are actually saying, find the MarkBehaviour in the prefab mark. This won’t work because prefab has not been instantiated, only a clone of the prefab has been.

You have two good ways of doing this.

FunctionR’s Solution 1

You can Instantiate the object then SendMessage:

GameObject g = Instantiate(mark, new Vector3(2.3f,-0.5f,0), gameObject.transform.localRotation) as GameObject;

g.SendMessage("setMark", markCount);

Notice I am keeping a reference g to the instantiated object so that I can use SendMessage on it.

FunctionR’s Solution 2

You can store a reference to the Component MarkBehaviour attached to the GameObject.

private MarkBehaviour markObj;

GameObject g = Instantiate(mark, new Vector3(2.3f,-0.5f,0), gameObject.transform.localRotation) as GameObject;
Once you have the reference to the GameObject g you can then use the GetComponent() function.

markObj = g.GetComponent<MarkBehaviour>();
markObj.setMark(markCount);

That is solution from mr FunctionR.

to change property/variable value from a other script
set a tag on the gameobject with you GameoverBehaviour script

create the new script and link it on the game object of your choice

public class MyNewSCript : MonoBehaviour {
	
	public GameoverBehaviour scriptLink;
	
	void Awake () {
		scriptLink = GameObject.FindGameObjectWithTag("myTag").GetComponent<GameoverBehaviour>();
	}

	void MyFunction() {
		gameoverBehaviour.markCount = 255;
	}
}