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 jmarchuk · Apr 28, 2017 at 06:03 AM · scripting problemcolliderscripting beginnernullreferenceexceptionontriggerenter

Object not responding to OnTriggerEnter()

So I'm trying to get a car to respawn at a general location some short time after passing a collider. For some reason, the script works if I walk into the collider with FirstPersonController. However it won't affect the car. Interestingly, if I test carReset() with "p" as shown in Update() after the car passes the collider, I get the error: "NullReferenceException: Object reference not set to an instance of an object". I'm fairly new to scripting and unity and I can't figure out why this wouldn't be working but still affects FirstPersonController.

Here's the script:

 public class CarRespawn : MonoBehaviour {
     GameObject car;
 
     void Update(){
         if (Input.GetKeyDown ("p")) {
             carReset ();
         }
     }
 
     void OnTriggerEnter(Collider c_car){
         car = c_car.gameObject;
         Invoke ("carReset", Random.Range (0.5f, 3.0f));
     }
 
     void carReset(){
         car.transform.position = new Vector3 (Random.Range (-6.0f, -12.0f), 1.16f, Random.Range(25.0f, 60.0f) );
     }
 }
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 JaredHD · Apr 28, 2017 at 08:24 AM

Ok, So this is what i did. You need 1 car with a Rigidbody. 1 Barrier with a box collider that has Is Trigger enabled. Then use the code below and attach that to the barrier.

 using UnityEngine;
 
 public class TriggerEnter : MonoBehaviour
 {
     GameObject car;
 
     private void OnTriggerEnter(Collider other)
     {
         if (other.name == "Car" || other.gameObject.tag == "Car")
         {
             Debug.Log("Hit");
             car = other.gameObject;
             Invoke("ResetCar", Random.Range(0.5f, 3f));
         }
     }
 
     private void ResetCar()
     {
         car.transform.position = new Vector3(Random.Range(6.0f, -12.0f), 1.16f, Random.Range(25.0f, -60.0f));
     }
 }

Note: The car must be named "Car" or have a tag called "Car"

That's it. It moves the car when it hits the barrier. I'm expecting that your problem was that the car didn't have a rigidbody.

The null reference exception was due to the fact that your car never actually registered as hitting the collider, and so it never assigned the 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
avatar image
0

Answer by jmarchuk · Apr 28, 2017 at 11:17 AM

@JaredHD Ah alright, thanks! So to be clear, the collider won't be detected unless the object it's attached to also has a rigidbody? Why is that?

Comment
Add comment · Show 1 · 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 JaredHD · Apr 30, 2017 at 10:55 AM 0
Share

@jmarchuk I'm pretty sure colliders won't be detected unless you have a rigidbody. Here's a website that explains how you can use a rigidbody to detect a trigger but ignore the physics, Here's another nice explanation as well. Basically as the second website says, Using Bampf answer,

"You can indeed have a collider with no rigidbody. If there's no rigidbody then Unity assumes the object is static, non-moving. Unity does not bother testing for collisions BETWEEN static objects. As you can imagine, this is very efficient if you have lots of scenery the player can bump into."

Hope that helps. Please don't forget to ensure that you accept the answer if it was right.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

OnTriggerEnter does not work 1 Answer

Destroying Multiple Game Objects w/ OnTriggerEnter 1 Answer

Trigger reference problem 1 Answer

NullReferenceException: Object reference not set to an instance of an object 2 Answers

Collider not registering hits? 4 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