I have a topdown 3d game and im trying to make it so that when the player clicks on “myTree”, myTree will dissappear and “WoodPile01” will appear on the ground. I keep am getting this error and not sure what it means or what to do to fix it.
var WoodPile01 : GameObject;
var hit : RaycastHit;
function Update() {
if (Input.GetButtonDown("Fire1")){
var ray : Ray = Camera.current.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit)){
if (hit.collider.gameObject.tag =="myTree"){
Instantiate (WoodPile01,hit.collider.transform.position,hit.collider.transform.rotation) ;
Destroy(hit.collider.gameObject);
print ("You Chopped Wood");
}
}
}
}
It works for me, have your trees been tagged “myTree”?, Have you attached this script to any gameObject in the scene, Is this a full script or only the part of it?
thats what i dont understand it worked for my FPS but its not working for this topdown. trees are tagged “myTree”. this script is attatched to the player gameobject. this is the full script.
I’ve made a simple scene with this script, attached. You may have to retag all the cubes as “myTree”(cubes are placeholders for trees, you have to retag because because tags can be messed up in unitypackage), and see if it works for you.