- Home /
I’m looking for a way to compare the speeds of two gameObjects on colliding. ,Hi, I’m looking for a way to compare the speeds of two gameObjects on colliding.
Hi, I’m looking for a way to compare the speeds of two gameObjects on colliding. So if the speed of the object is faster than the object it has collide with it ApplysBreak. How ever when I try and access the collison.speed I get a null reference.
private void OnTriggerStay2D(Collider2D collision) { Debug.Log("TriggerStay");
Traffic Col_speed = collision.gameObject.GetComponent<Traffic>();
if (speed > Col_speed.speed)
{
Accelerate = false;
ApplyBreak = true;
}
else if (speed< Col_speed.speed)
{
Accelerate = false;
ApplyBreak = false;
speed = Col_speed.speed;
},Hi, I’m looking for a way to compare the speeds of two gameObjects on colliding. So if the speed of the object is faster than the object it has collide with it ApplysBreak. How ever when I try and access the collison.speed I get a null reference.
private void OnTriggerStay2D(Collider2D collision) { Debug.Log("TriggerStay");
Traffic Col_speed = collision.gameObject.GetComponent<Traffic>();
if (speed > Col_speed.speed)
{
Accelerate = false;
ApplyBreak = true;
}
else if (speed < Col_speed.speed)
{
Accelerate = false;
ApplyBreak = false;
speed = Col_speed.speed;
}
}
Answer by CiberX15 · Apr 22, 2020 at 03:22 PM
It looks like you are trying to get the speed from your Traffic class, not the collision. If that is throwing a null ref, the object you are colliding with probably doesn't have a traffic component. You could try null checking Coll_speed before using it:
Traffic Col_speed = collision.gameObject.GetComponent<Traffic>();
if (Col_speed != null && speed > Col_speed.speed)
{
Accelerate = false;
ApplyBreak = true;
}
else if (Col_speed != null && speed < Col_speed.speed)
{
Accelerate = false;
ApplyBreak = false;
speed = Col_speed.speed;
}
That will never work if the things you are hitting never has a Traffic component though. If it is the thing doing the hitting that has the Traffic component and not the thing being hit you could do something like this instead:
Traffic Col_speed = gameObject.GetComponent<Traffic>();
if (speed > Col_speed.speed)
{
Accelerate = false;
ApplyBreak = true;
}
else if (speed < Col_speed.speed)
{
Accelerate = false;
ApplyBreak = false;
speed = Col_speed.speed;
}
Note that in the second example we get Col_speed from the local game object and not from the collision.
Answer by zainmallett · Apr 23, 2020 at 04:47 AM
Hi thanks for the help. I think the problem has to do with both objects being in the traffic class. So I'm trying to get the speed of the collider and the collision object from the same script. Which i don't think is possible?
Well what I’m trying to do is have cars travelling on the road. The cars have a trigger at the front and a hitbox at the back of the cars. What I want to happen is when the trigger hits the hitbox it compares the Variables for speed of the two cars in the collision, i.e. the make the car at the back slow down or equal the speed of the car in the front. However, what I am having trouble with is referencing the speed of both cars and comparing them. This is due to them both being prefabs using the same script. Which I’m learning means that I’m just referencing the same script and not the actual values of the individual car.
Your answer
![](https://koobas.hobune.stream/wayback/20220612232136im_/https://answers.unity.com/themes/thub/images/avi.jpg)