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 eguels · May 30, 2016 at 01:19 PM · instantiate prefabreferencing

Failing to reference an instantiated object

I am trying to instantiate an enemy object with some input parameters for the enemy's health and damage like this:

 if (Input.GetKeyDown(KeyCode.T))
         {
             GameObject enemy = Instantiate(whatToSpawn, this.transform.position, this.transform.rotation) as GameObject;
             enemy.GetComponent<Enemy>().changeStats(health, damage);
         }

it does spawn the enemy object, but it fails to create a reference to it, and I get "NullReferenceException: Object reference not set to an instance of an object" error in Unity. What am I doing wrong? I have looked at several threads where this is how people pass parameters to instantiated objects.

I have made sure that the newly instantiated object prefab does in fact have an Enemy component and that it contains the changeStats() method.

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 ninja_gear · May 30, 2016 at 02:35 PM 1
Share

What is 'whatToSpawn'? How are you defining that variable? What line of code does the error occur on?

It seems to me like there is nothing wrong with this code. I suspect that you are not saving enemy anywhere else after instantiating it, yet trying to operate as if you had. Enemy is local to that function. If you have 'enemy' anywhere else in the script, this function will not save this reference there, it will save it in the local one. If that is the problem, then this should fix it:

  if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.T))
          {
              enemy = Instantiate(whatToSpawn, this.transform.position, this.transform.rotation) as GameObject;
              enemy.GetComponent<Enemy>().changeStats(health, damage);
          }


edit: also, I believe you put the 'as' statement next to the 'whatToSpawn':

 enemy = Instantiate(whatToSpawn as GameObject, this.transform.position, this.transform.rotation);

avatar image eguels ninja_gear · May 30, 2016 at 02:47 PM 0
Share

whatToSpawn is declared outside the method like this:

 public Transform whatToSpawn;

Apart from this, the script has virtually no other code. Neither of your suggested code snippets solved the problem, unfortunately. The prefab does get instantiated, however, once I try to access the Enemy component, it says that it is missing a reference to the object. It's as if I would instantiate it as enemy GameObject, and then in the next line it no longer remembers this just created object. $$anonymous$$aybe you're right that I am not storing/saving it properly. If that's the case, I don't know how to do that.

3 Replies

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

Answer by ninja_gear · May 30, 2016 at 02:51 PM

make sure whatToSpawn is a GameObject, or your code will choke when attempting to cast with as and null will be returned

Comment
Add comment · Show 2 · 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 eguels · May 30, 2016 at 02:55 PM 0
Share

This solved the problem! Thanks a bunch!

That's strange, though. I always see people expose their game object variables as Transforms.

Thanks again!

avatar image ninja_gear eguels · May 30, 2016 at 06:09 PM 1
Share

Always save what you are going to use:

   if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.T))
   {
     Enemy enemy = Instantiate(whatToSpawn as GameObject, this.transform.position, this.transform.rotation).GetComponent<Enemy>() ;
     enemy.changeStats(health, damage);
   }
avatar image
0

Answer by Arsineh · May 30, 2016 at 02:45 PM

did you initialize your public variable in Inspector? probably when you create an object in script and it is public, you will see the variable in inspector and you need to give it a parameter to initialize.

Hope this can solve the problem

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 eguels · May 30, 2016 at 02:58 PM 0
Share

Yeah, the problem was that I stored the prefab as a Transform, and not as a GameObject. I'm still not sure why, though, Transform usually gets the job done, and I thought it would be castable as a GameObject anyway. It wasn't.

Thank you for giving it a shot!

avatar image
0

Answer by vittu1994 · May 30, 2016 at 01:58 PM

Try:

 GameObject enemy = (GameObject)Instantiate(whatToSpawn, this.transform.position, this.transform.rotation);
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 eguels · May 30, 2016 at 02:14 PM 0
Share

Now it says

 Cannot cast from source type to destination type.
avatar image vittu1994 eguels · May 30, 2016 at 02:38 PM 0
Share

Is the Enemy script attached to the enemy object you are trying to instantiate?

avatar image eguels vittu1994 · May 30, 2016 at 02:50 PM 0
Share

Yes, it is. It was even available as an auto-complete suggestion, along with the other scripts the prefab has.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GameObject variable points to instance instead of prefab when prefab gets instantiated 2 Answers

Particle System is not Playing 2 Answers

Instantiate an object around the center 0 Answers

Referencing all objects in scene 2 Answers

trouble with referencing a componenet (bool) 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