Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by zainmallett · Apr 22, 2020 at 09:55 AM · nullreferenceexceptioncollision detectionontriggerenter2dcompare

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;
     }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

132 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Enumeration Equals Method Causing : NullReferenceException: Object reference not set to an instance of an object 0 Answers

Two Different Box Colliders Aren't Colliding 1 Answer

"Private member OnTriggerEnter2D is unused"? 0 Answers

OnTriggerEnter2D & OnCollisionEnter2D Not Responding 1 Answer

Object is not colliding with wall colliders 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges