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 lamxung55 · Sep 20, 2014 at 10:31 AM · prefabinitiate

GameObject not null before Instantiate

Hi all, I have a problem with Initiate function: I declare a GameObject as a public variable in my C# script: public GameObject myGameObject; Then in editor I drag my prefab into my C# script position. I debug I recognize that before I call Instantiate(myGameObject), myGameObject has existed ( not null), Why?????

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 lamxung55 · Sep 20, 2014 at 10:51 AM 0
Share

Sorry for something not clear in my question.

I re-write my question:

I have a prefab(called myGameObject).

I want to create my prefab just when my custome event called:

 public GameObject myGameObject = null;
  On$$anonymous$$yEvent()
  {
    Instantiate(myGameObject);
  }

I think myGameObject will be null before I call Instanitate(myGameObject) but in fact it is not null.

Do I miss something here, please tell me. $$anonymous$$y final target is create myGameObject just when On$$anonymous$$yEvent called to save memory.

Thank you

1 Reply

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

Answer by dmg0600 · Sep 20, 2014 at 10:45 AM

When you create the variable GameOBject myGameObject and pass it a prefab, you are giving it a value, therefore it is not null.

What Instantiate does is to create a copy of the object you pass as a parameter, not to store a GameObject into that variable.

As an example:

 public GameObject myGameObject;
 
 void Start()
 {
     Instantiate(myGameObject);
 }

This will create a copy of whatever GameObject (from the scene or a prefab) you have dragged to myGameObject variable, but you will have no reference to it. If you want to keep a reference for the new copy you can do it like this:

 public GameObject myGameObject;
 private GameObject copyOfGameObject;
 
 void Start()
 {
     copyOfGameObject = Instantiate(myGameObject) as GameObject;
 }
Comment
Add comment · Show 10 · 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 lamxung55 · Sep 20, 2014 at 11:12 AM 0
Share

Thank you so much I have 2 questions here:

1.

When you create the variable GameOBject myGameObject and pass it a prefab, you are giving it a value, therefore it is not null.

==> So where myGameObject: I cannot see it in my hierachy or in editor view.

  1. How can I get my goal: just create myGameObject when On$$anonymous$$yEvent called to save memory?

Thank you

avatar image dmg0600 · Sep 20, 2014 at 11:51 AM 0
Share

In the script you have a variable called myGameObject and you are trying to instantiate it. This variable is not the destination of the new instance, is the source from where the instance is created.

If you want it to be the destination of the new instance, make it like this.

 public GameObject prefabToInstantiate;
 public GameObject myGameObject;
 
 On$$anonymous$$yEvent()
 {
     myGameObject = Instantiate(myGameObject) as GameObject;
 }

You will have to select to object which has this script in the scene or in the hierarchy. Its properties will appear on the Inspector window. Then you have to drag the prefab you want to instantiate from the Project window to the variable prefabToInstantiate in the Inspector window.

With this done you will have a variable prefabToInstantiate which will have the prefab (therefore it won't be null) and a variable myGameObject that will be null until your event triggers.

avatar image lamxung55 · Sep 20, 2014 at 01:27 PM 0
Share

Thank you but your way is not match to my goal. I want to save memory loaded, by your way I still waste prefabToInstantiate which is not null before event triggers. Any other idea

avatar image dmg0600 · Sep 20, 2014 at 03:29 PM 0
Share

If you want to Instantiate a prefab there is no other way than having a reference to it, as if you do execute Instatiate of a null variable it will instantiate nothing and give you an ArgumentException.

Of course, to have a reference, the varaible in which you have the reference cannot be null. This is a must.

The only way to do it differently is to have the prefab in Resources and load it at runtime but I think it is even more memory consu$$anonymous$$g.

 public GameObject myGameObject;
  
 On$$anonymous$$yEvent()
 {
     myGameObject = Instantiate(Resources.Load<GameObject>("YourPrefabName")) as GameObject;
 }

To do it like this, your prefab must be in a folder named Resources in Assets.

avatar image lamxung55 · Sep 20, 2014 at 04:17 PM 0
Share

As your answer, there's no way to create new game object when I need then destroy it when I don't need????

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to order newly created Objects? 1 Answer

[C#] An error occurs when trying to set a child to my GameObject in script. Why? 0 Answers

Animation plays (is visible) in scene but not in game 0 Answers

Prefab doesn't work when placed in another scene. 0 Answers

Changing value of prefab without effecting all instances. 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