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 /
  • Help Room /
avatar image
0
Question by Cripstat · Oct 29, 2015 at 08:08 PM · instantiatebeginnercreate

Need Help instantiating a Game Object Prefab.

So i need advice on how to create an instance of an object, it exists as both a Prefab and within the hierarchy currently in the scene, upon the scene being started the object destroys itself and I would like to bring it back while a keypress is down, and then have it destroyed upon key up.

At the moment I have the object present and can destroy it on key up however I cannot work out how to bring it back, I looked and tried multiple methods of "instantiating" but for whatever reason(most likely my own inexperience) I cannot get them to work, The root of the issue seems to be that within the script where i write my code for this, I cannot access the prefab. I used examples using this:

 GameObject newObject = Instantiate(circle.gameObject, objPos.position, objPos.rotation);

several things I do not understand: How to have this code use the prefab I want, when I try to add the prefab into the first parameter it does not recognize it, and also how do I give it a position, examples I saw use the second and third parameters straight off a transform that seemingly has no value?

Any help explaining this would be very much appreciated!

Oh also the Game Object I want to instantiate has no rigidbody as it is an empty object that is parent to 4 planes.

Comment
Add comment · Show 10
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 spooneystone · Oct 29, 2015 at 09:02 PM 0
Share

Need to see rest of script

avatar image Cripstat spooneystone · Oct 29, 2015 at 09:43 PM 0
Share

Well my question relates more to the general use of instantiate and how it communicates with prefabs/game objects rather than my specific code, Having tried many different examples I am really back at the start none the less this is what I have written so far:

 using UnityEngine;
 using System.Collections;
 
 public class playerControl : $$anonymous$$onoBehaviour {
 
     //Casting state
     private bool casting$$anonymous$$ode = false;
     public string castState = "off";
 
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
 
         if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.C)){
             casting$$anonymous$$ode = true;
             Cursor.visible = true;
             castState = "on";
 
         
         }
         if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.C)){
             casting$$anonymous$$ode = false;
             Cursor.visible = false;
             castState = "off";
 
         
             GameObject castInt = GameObject.Find("castInterface");
             castInterface castScript = castInt.GetComponent("castInterface") as castInterface;
             castScript.removeSelf();
 
         }


I have edited out the bits like movement control and the rest as they aren't relevant, I don't have an issue with my current code; it works perfectly, I am looking for the right way to add to it as mentioned in my post.

Thanks for the reply!

avatar image Statement · Oct 29, 2015 at 10:08 PM 0
Share
 GameObject newObject = Instantiate(circle.gameObject, objPos.position, objPos.rotation);

Will not compile because that overload of Instantiate return an Object, not a GameObject. You need to cast it with the explicit cast operator or the "as" operator..

 GameObject newObject = (GameObject)Instantiate(circle.gameObject, objPos.position, objPos.rotation);
avatar image Cripstat Statement · Oct 29, 2015 at 10:20 PM 0
Share

Thank you very much I will try this right now, further to getting the whole thing to work, if i create a transform at the start of the class for the instantiate to utilize for position and rotation how do I feed it a value, so far I have only seem empty transforms used in examples and I cannot work out why/how they would work?

Edit: I tried it with no luck I added the code as:

 if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.C)){
             GameObject newObject = (GameObject)Instantiate(castInterface.gameObject,intPos.position,intPos.rotation);    
         }

with the empty transform at the top like in the other examples, it returned 3 errors:

alt text

errors.jpg (36.9 kB)
avatar image Statement Cripstat · Oct 29, 2015 at 10:53 PM 1
Share

I have a empty game object that is the parent of four planes, I need to instantiate that group of planes as I am toggling them on and off. I currently can destroy them but I am stuck struggling with instantiate trying to bring them back.

$$anonymous$$ake sure the object you are instantiating exists. Usually Instantiate will use a prefab as a source (though it's not required). But if your source object is destroyed, you can no longer instantiate it (well, there's nothing to instantiate from, right?).

I dont know what requirements you have but I would just flick it on/off with thatGameObject.SetActive(true/false); etc.

Show more comments
avatar image Cripstat Statement · Oct 29, 2015 at 11:02 PM 0
Share

I am replying here as for some reason I cannot reply to your most recent post, I have made some progress in the last $$anonymous$$uet, by simply using instantiate with one parameter (the object I want to instantiate) I seem to be able to create clones however they appear as "prefab(clone)" in the the hierarchy and also do not respond to a script that was on the original prefab, I had a simple destroy(gameObject); that responds to a key pressUp and it works for the first, original instance of the prefab but not for the new instances I am creating... Also while the clones are appearing in the hierarchy I do not see the planes visually represented on the scene, is it possible they are only instantiating the empty prefab and not the planes or script? As you said it makes sense that I need something to instantiate from and the parent empty object is part of a prefab with the four planes, this should mean I don't need one to be active in the hierarchy to instantiate it right?

I will try setActive as a solution now but I do want to try and understand instantiate properly as I will inevitably need it in the future for projectiles etc.

Thanks again for all the help!

avatar image Statement Cripstat · Oct 29, 2015 at 11:06 PM 0
Share

If your prefab is a game object with 4 child game objects with a plane mesh each and you instantiate that prefab, you should expect to get exactly that after the instantiation. A clone with 4 children with a plane each.

is it possible they are only instantiating the empty prefab and not the planes or script

No. You most likely have selected the wrong prefab, or your prefab doesn't have the children. Perhaps you forgot to update the prefab.

I don't need one to be active in the hierarchy to instantiate it right?

No, you just need a reference to an asset in your project.

but I do want to try and understand instantiate properly

Good.

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

My Enemy Spawner spawns twice at once 0 Answers

I need help with Instantiate,I need help instantiating blocks! 0 Answers

Many loops to instantiate different prefabs during a given time 0 Answers

toggling ragdoll as new object 0 Answers

How to create Game Dev Tycoon/Story/Studio 2? 0 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