C# - My increament code is not working?
Hello,
I am trying make block breaker, in my code I add variable timesHit for record number of times ball hit the block which is increment when ball hit the block but does not show on Unity Editor.
using UnityEngine;
using System.Collections;
public class Brick : MonoBehaviour {
public int maxHits;
public int timesHit;
// Use this for initialization
void Start () {
timesHit = 0;
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D(Collision2D collision){
timesHit++;
print (timesHit);
}
}
The below timesHit++ in OnCollisonEnter2D method is printing correct but timesHit on Unity Editor does increment.!
[1]: /storage/temp/58267-script-snap.jpg
Answer by MakinStuffLookGood · Nov 17, 2015 at 07:01 PM
Hello and welcome to Unity Answers! First it would be helpful if you clean up the formatting of your post to make it a bit easier to read.
As for your question, this works for me exactly as you've posted it (Unity 5.2.1f1-64 bit). Are there any other behaviours at play here? Are you sure the brick you are looking at in the inspector is the one printing the timesHit
? Have you tried simply closing and reopening Unity? The editor can be janky at times and this indeed can fix some issues.
Glad to hear it, consider marking the answer correct so your question will appear solved.
Your answer
Follow this Question
Related Questions
2D collision not working 0 Answers
Collisions not working in top-down RPG? 0 Answers
How to check if any/multiple 2D sprites neighbour/collide with eachother 0 Answers
collision not working after enabling it twice 0 Answers
Collision not working 1 Answer