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);

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?

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 () {

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 :slight_smile:

edit : this is assuming that the script is attached to the tree.

The instantiate function takes no transform as parameter. You can use:

Instantiate (prefab, tree.position, tree.rotation); // or whatever position/rotation you want


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 :slight_smile:

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(){




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//