I have a script that adds an x amount of balls to the scene on start up.
void Start ()
{
for (int i = 0; i < amountBalls; i++) {
GameObject currBall = Instantiate (ball);
currBall.gameObject.name = "Ball: " + ballType [count % 4] + " " + (count / 4);
currBall.transform.SetParent (this.gameObject.transform, false);
currBall.transform.position = new Vector3 (4.38f, -1.6f, -4.37f);
currBall.GetComponent<Renderer> ().material.color = ballColors [count % 4];
currBall.AddComponent<Ball> ();
currBall.AddComponent<Collisions> ();
count++;
}
}
The goal: Have a string that keeps track of x collisions of x ball. Ex: “Ball 1: 2coll, Ball 2: 34coll, Ball 3 : 7coll”, ect.
Here is my script collisions (detect collisions for each ball) Collisions is attached to each ball that was created.
void OnCollisionEnter(Collision collision)
{
//if (collision.gameObject.name == this.gameObject.name){
if (collision.gameObject.name != "Shield") {
if (collision.gameObject.name != "Player") {
this.gameObject.GetComponent<Ball> ().numCollisions++;
//public int numCollisions { get; set; }= this.gameObject.GetComponent<Ball> ().numCollisions++;
//printAllBallInfo (collision);
}
}
}
}
Here is my script ball (ball is attached to each ball that was created) that counts collisions for each ball.
public string ballName { get; set; }
public int ballNumber { get; set; }
public int numCollisions { get; set; }
public int midiVelocity { get; set; }
public Color ballColor { get; set; }
void Update ()
{
print ("ball Name: " + ballName);
print ("numCollisions: " + numCollisions);
}
}
my problem is that the values of the collision are overwriting each other… ball 1 when collides will add to the value of collisions in ball 2
this is probably because of my use of public getter and setter, but don’t know how to do it otherwise.
Any help would be most appreciated!! Cheers!