- Home /
Question by
LenaDoll · Jul 25, 2017 at 02:16 PM ·
valuecounternull reference exceptionclones
How can I avoid null point exception and count clones? My counter only counts the original ball but not the clones. Help?
[CODE]
public int count;
public Text countText;
// Use this for initialization
void Start () {
count = 0;
SetCountText ();
Destroy (this.gameObject, 10);
}
// Update is called once per frame
void Update () {
}
void OnMouseDown() {
if(Input.GetMouseButton (0))
{
count += 1;
SetCountText ();
Destroy (this.gameObject);
}
}
void SetCountText(){
if (countText != null)
{
countText.text = "" + count.ToString();
}
}
} [\CODE]
Comment
Please show us the code, come on .. common thing when you ask a question like this.
Simplest, most naive way:
public int static count = 0;
public Text countText;
// Use this for initialization
void Start ()
{
SetCountText ();
Destroy (this.gameObject, 10);
}
And you removed the part of your Start() function that sets the value to 0?
Are you sure your Text reference is being preserved across the clones?
Here would be the next step in naive solutions. $$anonymous$$ake sure the first one has its text reference:
public static int count = 0;
public Text countText;
public static Text staticCountText;
// Use this for initialization
void Start () {
if (countText != null)
staticCountText = countText;
SetCountText ();
Destroy (this.gameObject, 10);
}
void SetCountText(){
if (staticCountText != null)
{
staticCountText.text = "" + count.ToString();
}
}
Your answer
Follow this Question
Related Questions
Detect cloned object detection once 0 Answers
Burgzerg arcade error with vital mods 0 Answers
Resouce script help 3 Answers
GuiText Score On Mouse Click 1 Answer
Roguelike Elements... 1 Answer