- Home /
How do I make my Instantiated prefab go where my game object moves ?
So basically, wherever my character moves, the prefab inherits the movement from it and is pretty much on top of it throughout.
Answer by coolbird22 · Mar 24, 2014 at 07:31 PM
Yes ! This got it working for me. Thanks a bunch !! Here is the working code :
if(Input.GetKeyDown(KeyCode.Space))
{
//Instantiating the prefab
cloneRadius = Instantiate(smashRadius, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
//Parenting the prefab to the game object
cloneRadius.transform.parent = transform;
cloneRadius.transform.localPosition = new Vector3(0, 0, 0);
}
Answer by mattyman174 · Mar 20, 2014 at 10:44 AM
You want to assign the new instantiated prefab as a child of the Character.
instantiatedGameObject.transform.parent = characterGameObject.transform;
Hi mattyman174, thanks for the reply though it doesn't seem to work for me. The code I'm using is this
public GameObject smashRadius;
void Update ()
{
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space))
{
GameObject radius;
radius = Instantiate(smashRadius, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
radius.transform.parent = smashRadius.transform;
}
if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Space))
{
Destroy (smashRadius, 0);
}
Where I want the prefab to instantiate in place of my game object when I press Space and destroy the prefab when I stop pressing Space. Thanks !
you need to store the instantiated GameObject somewhere where it can be accesed on another iteration of Update(). you're calling Destroy() on the prefab.
basically, make your variable "radius" a private field, and in your second if statement, change your line to Destroy(radius,0);.
hope this helps
Hi there. Thanks for replying back. On doing what you suggested gave me errors from some reason :S
Assets/Scripts/SpawnSmashRadius.cs(24,33): error CS0103: The name
radius' does not exist in the current context - Assets/Scripts/SpawnSmashRadius.cs(24,25): error CS1502: The best overloaded method match for
UnityEngine.Object.Destroy(UnityEngine.Object, float)' has some invalid argumentsAssets/Scripts/SpawnSmashRadius.cs(24,25): error CS1503: Argument #1' cannot convertobject' expression to type `UnityEngine.Object'
using UnityEngine; using System.Collections; public class SpawnSmashRadius : $$anonymous$$onoBehaviour {
private GameObject smashRadius; float speed = 5f; void Update () { if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space)) { GameObject radius; radius = Instantiate(smashRadius, new Vector3(0, 0, 0), Quaternion.identity) as GameObject; radius.transform.parent = smashRadius.transform; } //To destroy the prefab if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Space)) { Destroy(radius, 0); } } }
remove line l3 ("GameObject radius;").
add a line under line 5 ("private GameObject smashRadius;"): "private GameObject radius;"
your errors are caused by the fact that the scope of the variable "radius" is only within the first if statement; the variable doesn't "exist" anywhere else
Your answer
Follow this Question
Related Questions
How do I make prefab follow a gameobject?(C#) 1 Answer
How do I find another prefab based off a numerical variable? [2D, C#] 0 Answers
Add Motion Blur To Prefab - HELP 2 Answers
Instantiate a prefab and follow player position in runtime? 0 Answers
How do I instantiate a projectile along the path of a raycast? 0 Answers