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 valiorin · Feb 01, 2014 at 05:39 PM · instantiatenullobject-reference-errornullreference

Object Reference not set to Instance on Initializing Instatiation?

Hello everyone, this is my first time posting a question, although I've been stalking the forums for a few months as I teach myself Unity and C#.

So the problem is, I'm instantiating an enemy object and grabbing its specific AI sensor component. I then attempt to initialize a few variables, and it returns the infamous "Object reference not set to an instance of an object". It then, obviously, doesn't initialize the variables. Although the scene runs fine with no compile errors.

Here is the function

 void SpawnEnemies()
     {
         
 SensorAI newEnemySensor = (Instantiate (enemies[cycleColor], proceduralWaypoints[0].transform.position + new Vector3(0,1,0), Quaternion.Euler (Vector3.zero))as GameObject).GetComponent<SensorAI>();
         newEnemySensor.transform.parent = transform;
         newEnemySensor.waypoints = proceduralWaypoints;
     }

Any and all help is appreciated. Thanks!!

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

2 Replies

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

Answer by janzdott · Feb 01, 2014 at 05:59 PM

"Object reference not set to an instance of an object" means something is null and you're trying to access its methods/properties. You can't access methods/properties of something that doesn't exist. Either your GameObject is null, meaning enemies[cycleColor] is returning null, or the GameObject prefab doesn't have a SensorAI component. Try this to find out which is null.

 void SpawnEnemies() {
     GameObject newEnemy = (GameObject)Instantiate(enemies[cycleColor], proceduralWaypoints[0].transform.position + Vector3.up, Quaternion.identity);
     if (newEnemy == null) {
         Debug.Log("GameObject is null");
         return;
         //GameObject is null.  That means enemies[cycleColor] is null
     }
 
     SensorAI newEnemySensor = newEnemy.GetComponent<SensorAI>();
 
     if (newEnemySensor == null) {
         Debug.Log("SensorAI is null");
         //SensorAI is null.  Means your prefab doesn't have a SensorAI component
     }
 }
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 valiorin · Feb 01, 2014 at 06:09 PM 0
Share

AHH, yes, I have discovered my error. I'm searching for the component in the wrong place, as it is in the child of the main prefab object. Well sometimes you just need someone else to look at your code. And I'm A$$anonymous$$AZED at how quick the community here is! Thanks a bunch!!

avatar image janzdott · Feb 01, 2014 at 06:23 PM 0
Share

You're welcome. Glad I could help.

avatar image
0

Answer by KellyThomas · Feb 01, 2014 at 05:48 PM

I would suggest breaking this line down so that you can examine each part:

 SensorAI newEnemySensor = (Instantiate (enemies[cycleColor], proceduralWaypoints[0].transform.position + new Vector3(0,1,0), Quaternion.Euler (Vector3.zero))as GameObject).GetComponent<SensorAI>();

This could be broken down to:

 Vector3 position = proceduralWaypoints[0].transform.position + new Vector3(0,1,0);
 Quaternion rotation = Quaternion.Euler(Vector3.zero); // this could also be Quaternion.identity
 GameObject go = Instantiate(enemies[cycleColor], position, rotation) as GameObject;
 SensorAI newEnemySensor = go.GetComponent<SensorAI>();

Now if any errors occur you can isolate it's location to a smaller piece of code. I suggest setting a breakpoint or using Debug.Log() statements to confirm that each variable is assigned the expected value on each line.

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 valiorin · Feb 01, 2014 at 05:50 PM 0
Share

Thanks, that's a great idea, I'll try it out!

avatar image valiorin · Feb 01, 2014 at 05:57 PM 0
Share

Ah yes, I see now that, although everything in the actual Instantiation is working (as is visible by working enemies at runtime) that the GetComponent at the end is returning Null... I'm not, however, sure why this is happening? I think the problem might be that enemies[] is an array of GameObjects however I am trying to spawn prefabs...? And therefor should perhaps have an array of prefabs somehow?

avatar image KellyThomas · Feb 01, 2014 at 06:04 PM 0
Share

If the Instantiate() call is returning a game object then double check that it actually has a SensorAI component.

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

20 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

Related Questions

LineRenderer not instantiating on iOS or Xcode, Works fine in editor and Unity Remote 0 Answers

Raycast causes game to crash when shooting up 1 Answer

NullReferenceException on Photon Unity RPC Call 1 Answer

check if an instantiated opject is null 1 Answer

Unity null check 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