Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by crtz55 · Oct 23, 2016 at 09:08 AM · instantiatenullreferenceexceptionnull reference exception

Why cant i instantiate my ship? Error NullReferenceException: Object reference not set to an instance of an object.

Im sorry if its obvious, im pretty new to this. basically when the ship moves to far to the right or left it runs into warp or warp2. when it collides i want it to instantiate to the other warp. but i keep getting the Error NullReferenceException: Object reference not set to an instance of an object.

CODE:

 var warp : GameObject;
 var warp2 : GameObject;
 var ship : GameObject;
 
 function OnTriggerEnter2D(obj)
 {
   var name = obj.gameObject.name;
 
   if(name == "barrier1"){
     Destroy(gameObject);
     Instantiate(ship, warp.position, Quaternion.identity);
   }
 
   if(name=="barrier"){
     Destroy(gameObject);
     Instantiate(ship, warp2.position, Quaternion.identity);
   }
 }

Comment
Add comment · Show 1
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 TreyH · Oct 24, 2016 at 05:42 PM 0
Share

Try printing the names of ship, warp, and warp2 at the top of that function and see if any of them aren't assigned.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by lorenzo17 · Oct 23, 2016 at 02:03 PM

Hi, I think you forgot to declare the collider type on the function OnTriggerEnter

It should be like this:

 function OnTriggerEnter2D (other: Collider2D)
 {
 }
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 crtz55 · Oct 24, 2016 at 12:54 PM 0
Share

no im afraid thats not right. the "(other : Collider2D)" just gives me an error. im using the same "OnTriggerEnter2D (obj)" on my ship to take damage from spawn rock(clone)'s and its working fine.

avatar image
0

Answer by phxvyper · Oct 24, 2016 at 09:59 PM

Make sure that ship, warp, and warp2 are being set and are not null. It is highly likely that one of these variables is null, hense the NullReferenceException. (Learn to read exceptions, they give you a lot of information)

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

89 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

Related Questions

NullReferenceException 0 Answers

Null Reference Exception on Sprite Renderer 0 Answers

Instantiation Problem 0 Answers

Null Reference 1 Answer

How do I fix a Null Reference Exception (object reference not set to an instance of an object)? 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