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 CinnoMan · May 09, 2014 at 10:29 AM · instantiatenullreferenceexceptionprefab-instance

can't reference an instance of an object (prefab)

Hi all I've checked out other NullReferenceException questions, but I still don't understand why the following doesn't work:

 public class Problem: Monobehaviour{
 
 public GameObject spawnMarker;
 public GameObject mySpawnMarker;
 
 public void PreWaveEvent(){
    determineSpawnPoint();
    mySpawnMarker = Instantiate (spawnMarker, spawnPoint, Quaternion.identity) as GameObject;
    Debug.Log (mySpawnMarker.transform.position);
 }
 
 }

spawnMarker references a prefab I drag an drop into it in the editor.

What happens is the following:

an instance of spawnMarker is succesfully created, I can see it in game mode.

However, all attempts to reference it using "mySpawnMarker" fail and throw a Null Reference Exception (Debug.Log was just for debug purposes, I originally tried Destroy(mySpawnMarker) and others). I don't understand why, because the same approach works for other objects in my game (though there I'm not instantiating from a prefab, but from another object that's in the scene). Thanks for your help!

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

1 Reply

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

Answer by D3ltaVyyb · May 09, 2014 at 11:14 AM

Hmm... I've had a few NRE's with prefabs Instantiating it like: "~~~) as GameObject;" Try this:

 public class Problem: Monobehaviour{
  
 public GameObject spawnMarker;
 public GameObject mySpawnMarker;
  
 public void PreWaveEvent(){
    determineSpawnPoint();
    mySpawnMarker = (GameObject) Instantiate(spawnMarker, spawnPoint, Quaternion.identity);
    Debug.Log (mySpawnMarker.transform.position);
 }


Or this is how I do it. Have a prefab in your resources folder called 'spawnMarker', and disregard assigning it in the editor and just call it from script.

 public class Problem: Monobehaviour{
  
 public GameObject mySpawnMarker;
  
 public void PreWaveEvent(){
    determineSpawnPoint();
    mySpawnMarker = (GameObject) Instantiate(Resources.Load("spawnMarker"), spawnPoint, Quaternion.identity);
    Debug.Log (mySpawnMarker.transform.position);
 }
  
 }
Comment
Add comment · Show 3 · 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 D3ltaVyyb · May 09, 2014 at 11:29 AM 0
Share

Also I see you're using spawnPoint. If that deter$$anonymous$$eSpawnPoint(); isn't setting spawnPoint , you might have to assign it like: spawnPoint = deter$$anonymous$$eSpawnPoint() or however you have it set up.

avatar image CinnoMan · May 09, 2014 at 12:38 PM 0
Share

Thanks for your answer: The approach with the resources folder solved the problem.

For completeness' sake: I had tried the "(GameObject) Instantiate..." approach before but that gave me an "InvalidCastException: cannot cast from source type to destination type".

deter$$anonymous$$eSpawnPoint() was working ok, I left some stuff out in the code example so it wouldn't distract from the core issue.

avatar image D3ltaVyyb · May 09, 2014 at 01:36 PM 0
Share

Good you're welcome, always glad to help.

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

22 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

Related Questions

Accessing a prefab after instantiating results as null 1 Answer

instantiated GameObject Prefab jumps in air after instantiate 1 Answer

NullReferenceException 2 Answers

How to manipulate a variable of a prefab script (instantiated) while the game is runnning . 1 Answer

Problem when setting variable in an instantiated gameobject 0 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