• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Action activates trigger. 1 Answer

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

Have something be a trigger and a collider? 1 Answer

Other function that is similar to OnTriggerStay 3 Answers

Destroy trigger so animation only plays once. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges