- Home /
Instantiate GameObject at Parent Transform
hey guys, just a simple one... ive attached this script to a tree, and i want it so that when clicked, the tree is destroyed and instantiated in its place is a prefab of some logs.
this is my script:
var prefab : GameObject;
var tree = Transform;
function OnMouseDown () {
Instantiate (prefab, tree);
Destroy(gameObject);
}
but it says that "No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEngine.GameObject, System.Type, int)' was found." can you guys help?
Answer by bubzy · Sep 02, 2013 at 06:14 AM
using UnityEngine;
using System.Collections;
public class makeLogs : MonoBehaviour {
// Use this for initialization
public GameObject logs;
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.A))
{
Instantiate(logs,transform.position,Quaternion.identity);
Destroy(gameObject);
}
}
}
this is c# but you should get the idea, the keypress is just to simulate your condition for activating the instantiation and destruction the error you are getting is because you have not correctly filled out the parameters of the instantiate
command. the unity script reference site will help you with this :)
edit : this is assuming that the script is attached to the tree.
Answer by InfiniBuzz · Sep 02, 2013 at 06:15 AM
The instantiate function takes no transform as parameter. You can use:
Instantiate (prefab, tree.position, tree.rotation); // or whatever position/rotation you want
or
Instantiate (gameObject);
this makes a clone of the gameObject. (just for the record)
also the line
var tree = Transform;
is probably not what you want. did you mean
var tree : Transform;
? before using the tree variable you should assign it. Sorry I don't see the point of this script but I hope it helps :)
Answer by Panik.Studios · Dec 12, 2013 at 05:40 PM
I know this is an old as thread too but someone might have a similar question in the future so! Here is my crack at it.
var Tree : GameObject;
var Logs : GameObject;
var LogSpawn : Transform; //This is an empty you attach to the tree//
function Update(){
if(Input.GetMouseButtonDown(0)){
Destroy(Tree);
SpawnLogs();
}
}
function SpawnLogs(){
Instantiate(Logs, LogSpawn.transform.position, LogSpawn.transform.rotation);
}
//You might need it to Be Logs.transform! You might also need it to say Destroy(Tree.transform) i cannot remember I use an object pool its a bit different with that//
Your answer

Follow this Question
Related Questions
Instantiate as a child at position 2 Answers
[SOLVED] Only instantiating once 1 Answer
swap objects with instantiate 2 Answers
How to correctly convert Object to GameObject 1 Answer
Bullet Instantiation 1 Answer