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 andrewwebber25 · Jun 21, 2017 at 05:49 PM · transformerror messagetransform.positionplatformerrespawn

Error says object reference not set when it really is?

Hey guys I feel like Ive tried everything here and I cant get this error to go away. Im making a 2D platformer and would like to add some spikes to the game. If the player collides with them, they "die" and respawn at the beginning of the map. I placed the spike sprite in the game and attached my "Hazard" script to it (no debugging errors what so ever). The spike "Is Triggered" when the player (who has the tag "Player") walks into it. I created an empty game object called "start" and placed it at the beginning of the map. This is where the player will transfer to when they collide with the spike. I drag the "start" object and attach it to the Hazard Script so that they are linked together.

When I press play, the player moves fine and as soon as it touches the spike, it flashes the error "NullReferenceException: Object reference not set to an instance of an object". I reference the start object though so I dont understand what it means by it not being set?! Is there something else that it is having difficulty with?

Here is the Hazards Code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Hazard : MonoBehaviour {
 
     private Controls player;
     public Transform start;
 
     void Start()
     {
         player = FindObjectOfType<Controls>();
     }
 
     void Update()
     {
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Player")
         {
             player.transform.position = start.position;
         }
     }
 }

Lastly, here is the inspector of my Player, idk if it can help at all. There is also a sprite rendered and animation in its inspector but I couldnt fit it in the snippet. The

Seriously, any help would be amazing. I feel like ive tried so many things and I cant get it to work. Thanks guys!

capture.jpg (127.6 kB)
capture.jpg (48.1 kB)
Comment
Add comment · Show 4
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 Habitablaba · Jun 21, 2017 at 05:55 PM 1
Share

Here's a question: is your 'player' object being found properly in Start?
What if you don't store that and you get the component from the 'other' object in OnTriggerEnter2D. If it has the tag "Player" it likely has the component too, right?

avatar image andrewwebber25 Habitablaba · Jun 21, 2017 at 06:42 PM 0
Share

Thank you so much for your help! Seriously really appreciate it!

avatar image Dave-Carlile · Jun 21, 2017 at 06:05 PM 1
Share

As @Habitablaba said, most likely player isn't set properly. Why not make that a public property and set it in the inspector like you do with start?

avatar image andrewwebber25 Dave-Carlile · Jun 21, 2017 at 06:43 PM 0
Share

Thank you so much for your input, definitely pointed me in the right direction!

1 Reply

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

Answer by Laiken · Jun 21, 2017 at 06:24 PM

Drag you "Controls" script to any game object in the scene in case you didn't. (if you didn't do it, the script is in your asset folder but not in the scene, that's why FindObjectOfType won't find it )

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 andrewwebber25 · Jun 21, 2017 at 06:41 PM 0
Share

Thank you so much! Seriously! Now that I see it, it makes total sense. Ive been jumping between a couple tutorials and I must not have had the script attached properly!

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

80 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

Related Questions

Getting an object to re-spawn/ transform player back to the beginning 1 Answer

Update instantiates more than 1 object 2 Answers

transform.position not setting position OR animation setting position even though it shouldn't 0 Answers

[2D] Get the position of an object outside the scope 2 Answers

moving objects with transform position 2 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