- Home /
Collision not changing Variable
I have a boolean saying where a ball is going to go. When the ball hits an object, it chnages the variable and goes the other way. when it collides, the debug says it collided, but the bool isn't changed.
using UnityEngine;
using System.Collections;
public class BallMovement : MonoBehaviour {
float x;
float y;
bool d;
Rigidbody rb;
void Start () {
rb = GetComponent<Rigidbody>();
}
void Update () {
if (d) {
Debug.Log ("true");
}
Move ();
}
void Move(){
if (d){
x = x + 0.1f;
} else {
x = x - 0.1f;
}
Vector2 v = new Vector2 (x,y);
rb.MovePosition (v);
}
void OnCollisionEnter(UnityEngine.Collision col){
Debug.Log("collided");
if (col.gameObject.name == "P1C1") {
d = true;
Debug.Log("collided");
}
}
}
the debug never says "true", and the ball doesn't change direction. I will eventually use an integer for d.
$$anonymous$$ake sure the object collided has the tag P1C1.
Change the text of the Debug.Log line 32 by "P1C1 collided" and see if this message appear.
Okay, now it is saying collided twice, even though i changed it to "P1C1 collided"
both your debug lines are the exact same which could easily be confused. Are you seeing 2 'collided' messages for each collision? Change line 32 to Debug.Log("P1C1 collided") like CrazyNinja said and make sure you see that in the console.
It's normal @CrazyNinja2000 since you have one "default" Log message saying you have collided and one Log message telling you a P1C1 object has been collided.
Answer by CrazyNinja2000 · Jul 01, 2015 at 11:49 AM
Okay, i found the problem. For the box collider, I have it as a trigger, whcih i guess made it not collide. It was colliding with other object on it's sides, P1C2 and P1C3 which were not triggers. Everything is working now.
Your answer

Follow this Question
Related Questions
Change Boolean with collision problem 2 Answers
triggering 3DText by collision 2 Answers
How can I tell the player is in contact with the ground, within a script associated with the player? 1 Answer
How do you trigger a collider to become enabled after the player collider isn't touching it anymore? 4 Answers
trigger is not working 0 Answers