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.
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!
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);
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:
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.
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!
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612043226im_/https://answers.unity.com/themes/thub/images/avi.jpg)