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 Cereal_Killa · Jan 18, 2015 at 07:13 AM · c#2dinstantiateprefab

The prefab you want to instantiate is null.

This is driving me insane. I've looked at multiple tutorials and the Unity documentation for instantiation.

I just want to instantiate an object when I call it's function.

The documentation tells me that I must create an empty game object in order to link the script to a prefab. So I've done this and added my Script that Instantiates the prefab and the prefab itself to this empty object.

alt text

Then from another script I call the script that Instantiates the object like so:

 new SpellObject();

My SpellObject code does this:

 public class SpellObject : MonoBehaviour {
 
     public Transform spellPrefab;
     public SpellObject(){
         Instantiate (spellPrefab, new Vector2(0,0), Quaternion.identity);
     }
 }

It should add this prefab:

alt text

I don't see how the prefab is null; it is clearly in the list with all my other scripts and items.

spellprefab.png (32.5 kB)
emptyobject.png (27.9 kB)
Comment
Add comment · Show 5
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 Cereal_Killa · Jan 18, 2015 at 07:29 AM 0
Share

Ok so the script now has an option to add a transform that wasn't there before, so I've added it there and deleted the empty game object. This has no initial errors and I can run the program, but as soon as I call the script, I get the errors pop up again:

alt text

unityerror.png (43.3 kB)
avatar image mattyman174 · Jan 18, 2015 at 07:40 AM 0
Share
  public SpellObject(){
 Instantiate (spellPrefab, new Vector2(0,0), Quaternion.identity);
 }

You should not be implementing a constructor in a monobehaviour.

Use the given function callbacks for initialization (Awake(), Start()).

avatar image Cereal_Killa · Jan 18, 2015 at 09:08 AM 0
Share

I've copied this to the Start() process, but it doesn't seem to like it:

alt text

start.png (7.9 kB)
avatar image richyrich · Jan 18, 2015 at 01:42 PM 0
Share

@Cereal_$$anonymous$$illa As others have stated/implied, you cannot use the new keyword to create objects of classes that inherit from $$anonymous$$onoBehaviour. Trying to place a constructor in the Start function will not help you. A class that inherits $$anonymous$$onoBehaviour (e.g. SpellObject : $$anonymous$$onoBehaviour) defines a component for a GameObject, therefore it does not make sense for it to be created on its own, so Unity won't let you.

If you want objects where you can use the new keyword, they must not inherit from $$anonymous$$onoBehaviour.

avatar image Cereal_Killa · Jan 19, 2015 at 02:28 AM 0
Share

If I remove $$anonymous$$onoBehaviour then I cannot use Instantiate. And I do want to create a brand new GameObject with no properties then add to it.

Would it be best to create a GameObject with hundreds of empty properties and disabled Components, and then set/reactivate them depending upon player choice?

Also I would like to be able to create more than one at a time, so should I create children rather than new copies of the original?

2 Replies

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

Answer by samizzo · Jan 18, 2015 at 10:28 AM

You can't allocate a new MonoBehaviour class using 'new', just like the Unity error is telling you. I'm not sure exactly what you're trying to do. Get rid of that SpellObject class. From the script where you want to create your spell prefab, make a property like so:

 public GameObject SpellPrefab;

Then in the editor assign the SpellPrefab prefab from the asset browser to that property. Then instead of doing 'new SpellObject' do:

 Instantiate(SpellPrefab, Vector3.zero, Quaternion.identity);
Comment
Add comment · Show 6 · 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 Cereal_Killa · Jan 18, 2015 at 10:35 PM 0
Share

Thanks for the help. I might take a step back from my project and try to learn from scratch the best way to organise my project and lay out my code with unity.

avatar image Cereal_Killa · Jan 19, 2015 at 12:55 AM 0
Share

FYI what I was trying to do was to create a new empty GameObject from code, then add Components and properties to the object depending upon player choice.

Like cooking - when the player gets out a bowl and starts adding flour and sugar, I don't know if it will be a cake or muffins or something else, so I wanted to just start with an 'empty bowl' so to speak.

I shouldn't have to create and link objects and scripts in the inspector - I think this confuses the issue.

Hence, I did want to instantiate a 'null/empty' game object, then modify and add to it. Is this possible? Or would it be best to create a GameObject with hundreds of empty properties and disabled Components, and then set/reactivate them depending upon player choice?

avatar image samizzo · Jan 19, 2015 at 01:07 AM 0
Share

Yes that's definitely possible. Let's say you have your 'init' script on some GameObject you placed in the scene. You can then do this:

 GameObject go = new GameObject("someObj");
 SpellClass newComponent1 = go.AddComponent<SpellClass>();
 SomeOtherClass newComponent2 = go.AddComponent<SomeOtherClass>();

That will create a new GameObject, give it a name "someObj", and add two components to it. The call to AddComponent also returns a reference to the newly added component (newComponent1 and newComponent2). You can then configure the new components as you like.

However, if you are creating them dynamically then the properties on those new components will be at their default values. You can't configure their values in the editor.

I think you might need to take a step back and work through some Unity tutorials to get a better feel for the workflow, and think about how you might want to structure your project. You'll probably want to use prefabs somewhere along the way.

avatar image Cereal_Killa · Jan 19, 2015 at 02:38 AM 0
Share

Thanks for the advice. So the GameObject will be called "someObj" within the inspector, but if I want to refer to it in code, it's actual name is "go"?

Thus the inspector name is more of a description?

Also if I want to create multiple go's, can they be named go1, go2, etc automatically depending on how many are created if I want to refer back to them later? Or can I not dynamically generate variable names. Is there a better way to keep track of different instances like an ID number?

If you could recommend a good tutorial or book on designing a project from a to b with good class use, I'd be really appreciative.

avatar image samizzo · Jan 19, 2015 at 02:44 AM 0
Share

Yeah the name of the object in the hierarchy will be "someObj" but the variable name in code is "go". But that could be anything else, and you could refer to it by another name later if you have another reference to the same GameObject.

You could keep track of the GameObjects that you create in an array or a list. You can dynamically create as many as you want but it would be a bit unwieldy to create hundreds of variables called go1, go2, go3 etc.

It sounds like you should do some C# tutorials too, but I don't really know of any, sorry. There's a lot of stuff on the Unity web site though. $$anonymous$$aybe take a look through there.

Show more comments
avatar image
0

Answer by jenci1990 · Jan 18, 2015 at 07:34 AM

If you create a new SpellObject(), the spellPrefab is null! Use parameter when create a new SpellObject:

 new SpellObject(spellPrefab);

and:

 public class SpellObject : MonoBehaviour {
     public Transform spellPrefab;
     public SpellObject(Transform _prefab){
         spellPrefab = _prefab;
         Instantiate (spellPrefab, new Vector2(0,0), Quaternion.identity);
     }
 }


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 Cereal_Killa · Jan 18, 2015 at 09:05 AM 0
Share

I've copied this directly, and created a new "public Transform spellPrefab;" in my calling class, but there is no place to assign the Transform to this script like before..

alt text

no prefab.png (38.4 kB)

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

29 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

Related Questions

How to generate different diagonal platforms? 0 Answers

2D Projectile Not Firing Based on Rotation 1 Answer

Why is my instantiated prefab invisible ? 2 Answers

How to generate different diagonal platforms? 1 Answer

Why is my Prefab Instantiating when the Scene is Loaded? 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