# How to make collisions occur between objects using the same script?

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. But how could I get round this? Is it possible to have collisions occurring between objects using the same script?



private void OnTriggerStay2D(Collider2D collision)
{
Debug.Log("TriggerStay");

GameObject collidedParent = collision.gameObject.transform.parent.gameObject; // get parent transform and then its gameobject
Traffic Col_speed = collidedParent.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;
}

if (transform.position.y > collision.transform.position.y)
{
turn = turn + 1;
Debug.Log(this.gameObject.name + "someMessage");

}
if (transform.position.y < collision.transform.position.y)
{
turn = turn - 1;
Col_speed.turn = 0;
Debug.Log(this.gameObject.name + "someMessage");

}
}
private void OnTriggerExit2D(Collider2D collision)
{
turn = 0;
Accelerate = true;
ApplyBreak = false;
}

}


The point is that you are not actually referencing the scripts but its intances. Each of your script components it technically already an instance of the script which means it can have its own values. In other words: You can have many cars using the same scripts. They individually do what the scripts tell them. They are instances and thats why it's even possible to have multiple cars that do individual things.

Traffic Col_speed = collision.gameObject.GetComponent<Traffic>();


should already get the instance of the other (collding) car so the Col_speed.speed is the other cars speed.

I hope this clears things up but what is your actual issue? -> describe what you expect but what happens instead, then the community can provide further help.

Edit:

As of your comments, to get the Traffic component from the parent you can do:

...
GameObject collidedParent = collision.gameObject.transform.parent.gameObject; // get parent transform and then its gameobject
Traffic Col_speed = collidedParent.GetComponent<Traffic>();
...


Note that this approach will stop working if you change the gameObject hierarchy.

• Well I'm glad i'm actually coding for the instance correctly. The issue is that i'm getting a error message saying "NullReferenceException: Object reference not set to an instance of an object TriggerHit.OnTriggerStay2D " for the line: if (speed > Col_speed.speed) so something is wrong – MrZeeIndo Apr 23 at 7:45
• Are the collider component and the Traffic ScriptComponent attached to the same gameObject? Or is one on the parent and one on a child gameObject? This would explain why the Traffic component cannot be found on the colliding gameObject. – schneebuzz Apr 23 at 7:51
• The Traffic ScriptComponent is attached to the parent, whilst the collider component is attached on a child gameObject. – MrZeeIndo Apr 23 at 7:58
• Thats why you are getting the error. The GetComponent<Traffic>() call searches the child gameObject (the one with the collider). Since there is no Traffic component on this gameObject, it returns null. Then when you try to get the speed on that null reference you get the NullReferenceException. To avoid this the simplest way is to do: collision.gameObject.transform.parent.gameObject.GetComponent<Traffic>(). I'll update my answer. – schneebuzz Apr 23 at 8:03
• That’s great thanks, your explanation makes so much sense and I’m no longer getting a null reference. However, I’m having another issue. The variables I am changing is applying to both objects that are collided. I don’t want it to apply to the collision object. – MrZeeIndo Apr 23 at 8:24