- Home /
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;
}
}
Is the sprite renderer on the same object as the $$anonymous$$arkBehaviour class?
Yes, in other class, I have an GameObject that link to $$anonymous$$arkBehaviour class. From other class, I called set$$anonymous$$ark() function successfully!
But the markCount property still not changed in Update() function of class $$anonymous$$arkBehaviour, how can I do that?
I see nothing wrong in this code. First are you sure you only have one game object with the above script? Second can you update the above code to include the whole class so we can verify any outer used of markCount? Also make markCount private.
I have added code of the class that call set$$anonymous$$ark function. Please see and give some solution, thanks!!
Answer by ngnclht · Jul 29, 2014 at 04:39 PM
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.
Answer by MaxOR PrimE · Jul 28, 2014 at 05:28 PM
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;
}
}
Answer by flaviusxvii · Jul 28, 2014 at 05:15 PM
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).
Your answer
