Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Merlingoth · Jan 24, 2011 at 11:50 PM · collidertriggerplayerreference

Script works for NPCs, but not Player

I'm developing a racing game, and as the cars pass through the finishline trigger it activates the script Finishline.js, each racer also has the script Stats.js attached to it. When the NPCs run through the trigger it activates Finishline.js which then changes the value inside Stats.js. It works fine for the NPCs but not for the player. There is nothing that I can see differs in this circumstance between the player and the NPC. Please help!

//Finishline.js function OnTriggerEnter (other : Collider) { var currentplayer : Stats; currentplayer = other.gameObject.GetComponent(Stats);

 if(currentplayer.currentlap==1 && currentplayer.valid)
 {
 //does action blah blah
 }

}

//Stats.js public var currentlap : int = 1; public var valid : boolean = false;

Thanks in advance! =]

Edit: The actual collider triggering works. I get an Object reference not set to an instance of an object error.

Comment
Add comment · Show 2
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 DaveA · Jan 25, 2011 at 12:29 AM 0
Share

What line is that error on? Does your player object have a collider on it?

avatar image Merlingoth · Jan 25, 2011 at 02:59 AM 0
Share

Yeah, it collides and triggers fine. The error is on the line with the if statement. Another weird thing is that I have another object that is supposed to disable the renderer onCollision and it only disables it when the player hits it and not the NPCs. I can't win! haha

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Molix · Jan 25, 2011 at 03:23 AM

If you are getting "Object reference not set to an instance of an object", then that means that the "currentplayer" object is null, i.e. it was not found in the "other" game object.

First off, add a check after GetComponent, e.g.:

if( currentplayer == null ) { // uncomment the logging line below for debugging only // Debug.Log("No Stats component found in " + other.gameObject.name );

return; }

If you're sure there is a Stats component in the player (and not just the NPCs), then it could be that there are multiple colliders/gameobjects that make up the player. So you might, for example, be looking for the Stats component in the wheel gameobject rather than the body. Assuming you put the Stats component in the root object, you could instead do:

currentplayer = other.transform.root.GetComponent(Stats);

That would check the root/topmost object in the other object's hierarchy for the Stats component. I hope that helps.

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 Merlingoth · Jan 25, 2011 at 03:35 AM 0
Share

Stupid me! It needed to be in the root object. Thanks, man. You rock!

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

No one has followed this question yet.

Related Questions

When "Player" enters a trigger = flashlight off 1 Answer

Action activates trigger. 1 Answer

Can't figure out how to use multiple triggers in single scene 1 Answer

Make trigger be activated only by the player 2 Answers

Can requirecomponent specify IsTrigger on a collider? 1 Answer


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