• 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 iocp · Dec 02, 2016 at 06:18 PM · gameargumentexception

The Object You want to instantiate is null?

I keep receiving this error when I try to instantiate a prefab. Yes, I have linked prefab to the GameObject inside the C# script and yet it still gives me this error. Yes I have tried Resource.Load as well and that also failed. The prefab is in my assets folder. this is my code so far.

 public class Surface : MonoBehaviour
 {
 
     public GameObject SurfacePrefab;
     public List<Entity> Players;
     void Start ()
     {
         Players = new List<Entity>();
 
         Instantiate(SurfacePrefab);
 
         Entity Player = new Entity();
         Player.CreateGameObject();
         Players.Add(Player);
 
     }
     void Update()
     {
 
     }
 }
 

Player.CreateGameObject(); is where the exception occurs.

 public void CreateGameObject()
     {
         Instantiate(EntityPrefab, Position, Quaternion.identity);
     }

And EntityPrefab is linked to the prefab which is just a sphere. alt text

link.png (66.0 kB)
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 roman_sedition · Dec 02, 2016 at 06:40 PM 1
Share

I think your problem is

 Entity player = new Entity();


What you want to do is make a reference to the Entity component which is on your object and not a new one.

 Entity = GetComponent<Entity>();

(also you should probably make your variables in camelCase)

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Bunny83 · Dec 03, 2016 at 12:56 AM

It looks like your "Entity" class is actually a MonoBehaviour. MonoBehaviours can not created with "new". So those lines doesn't make any sense:

 Entity Player = new Entity();
 Player.CreateGameObject();

Even when you could create an Entity with new, it's EntityPrefab variable would not be set to anything.

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
avatar image
1

Answer by CJMaeder · Dec 03, 2016 at 09:39 AM

Hi @iocp, I would try something like this instead.

 public class Surface : MonoBehaviour
 {
 
     public GameObject SurfacePrefab;
     public GameObject EntityPrefab;
     public List<Entity> Players;
     void Start()
     {
         Players = new List<Entity>();
 
         Instantiate(SurfacePrefab);
 
         CreateGameObject();
 
     }
     void Update()
     {
 
     }
 
     public void CreateGameObject()
     {
         GameObject player = Instantiate(EntityPrefab, Position, Quaternion.identity) as GameObject;
         Entity playerEntitiy = Player.GetComponent<Entity>();
         Players.Add(playerEntitiy);
     }
 }

Ofcourse you would then have to edit your prefabs in the editor.

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
avatar image
-4

Answer by studioAdam · Dec 02, 2016 at 10:42 PM

Hi @iocp,

If you're instantiating a GameObject you'll need to cast is as such by adding 'as GameObject' after the instantiate call. i.e:

 public void CreateGameObject()
      {
          Instantiate(EntityPrefab, Position, Quaternion.identity) as GameObject;
      }

Hope you get you're resolution.

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 hexagonius · Dec 02, 2016 at 10:51 PM 2
Share

it is not necessary if you don't need a reference to it and the missing cast is not the reason for the NullReference.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

getting udp package info inside unity (GlovePIE) 0 Answers

How to compare if two gameObjects are the same? 2 Answers

Can't see underneath terrain. How do I fix this? 1 Answer

Help Me with My Script Pls?????? 1 Answer

how i can import game source code to unity? 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