Brian Nguyen
Brian Nguyen

Reputation: 485

How can I update a property of MonoBehaviour's script?

I have a MarkBehaviour class that contains a property called markCount. Depending on markCount, I will select the sprite on the Update() function.

But when I call the setMark() function from other behaviour class, I have logged the markCount property in 2 functions below, but the markCount property on Update() function was not changed. It was changed only in setMark() function.

public class MarkBehaviour : MonoBehaviour {

    public int markCount;

    void Start(){
           markCount = 0;
    }

    void Update () {    
        Debug.Log ("mark setted from other class" + 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 change it?

Code of the other class that calls setMark() function:

public class GameoverBehaviour : MonoBehaviour {

    public GameObject mark;

    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;
    }
}

Upvotes: 1

Views: 435

Answers (2)

axwcode
axwcode

Reputation: 7824

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.

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.


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);

Upvotes: 2

Nergon
Nergon

Reputation: 453

i think the problem is that you are not creating a proper reference to the mark instantiation.

replace this :

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

with this :

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

Upvotes: 1

Related Questions