One tree being chopped by player all trees get chopped help?

Hey guys.

I understand what objects are, and instances of the objects… in other engines that is. In unity I’m a bit confused. So heres my story for today and questions I’ve got.

I have a prefab of a tree object that I made. Has a script “Chop_Tree”. I also have a prefab of my Player, with a Script Player(for when I make added animations and so forth I can port my scripts over rather easy if I get side-ways some how).

I’m going to try different approaches tomorrow to solve my problem, but I just wanted advise and perhaps tips before I do.

Here is most likely where I went wrong. In the Chop_Tree script I have it linked to the player in the inspector to make sure the player is within a certain distance to the tree to enable him to chop it(its a click to move 3rd person view kind of game - so it disables his move for the time period unless double clicking instead of single to leave). When he starts to chop the one tree - ALL the trees animate as if being hit by the player also, all fall as well… heh.

I’m a bit unsure how to approach the correction to this problem since as I said my prefab is linked to the player.

My first approach is going to be to add onto the script to of Chop_Tree a distance check on the tree that the player is truly by, and if hes not but the other trees ignore/exit… but this sounds like its going to be very slow if I start to get a lot of trees down, and doesn’t seem to be the best method. I’m not really to comfortable with arrays only been programming for 7 months or so, so perhaps its time to learn how to use them to assist in this?

I’m over all wondering a few things.

  1. What are common approaches used to check for situations like this to apply only the wanted out come to one instance of a object?
  2. Instead of iterating all trees, to make sure that only the one the player is by is changed, would it be faster to perhaps add the only tree I’m changing to some type of list?

Just looking for a little guidance and knowledge to advance myself, I feel a bit confused by this one.

As always thanks community.

When you call Chop_Tree, you end up calling it on the prefab, and it changes for every instance of that prefab. The way I'd try first would be to add a trigger collider on the player, with a rather short range, that is triggered when getting close to the trees. (You could add a tag to the tree prefab to check if the object is really a tree.) The trigger will receive the GameObject as a parameter, the exact instance you want to chop. Then you will be able to call the Chop_Tree for that specific instance of a tree.

2 Answers

2

Add a Sphere Collider to your character and set it as a trigger. When you get in range of the tree, it will enter the trigger zone and send the tree’s reference to your trigger function:

void OnTriggerEnter(GameObject other)
{
    if(other.gameObject.tag == "Tree")
    {
        other.GetComponent<Chop_Tree>().Animate();
    }
}

Then, you can check that the collider is really a tree and call this collider’s function to chop it. The other GameObject that is sent to the function is the reference to only the tree that collided, so only this tree will be chopped.

http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnTriggerEnter.html

This page explains how to use the colliders and there is a nice table at the bottom showing which kind of GameObject send which trigger when colliding with other GameObjects:

http://docs.unity3d.com/Documentation/Components/class-BoxCollider.html

Good stuff - been reading up on stay/enter events and seeing which would be best - either way thanks for the tips and so forth. I'll be looking over the second doc area I've already been poking my head around in the trigger collider.

Have a look at: “How to access a variable or method on another script or object” (Unity Gems: Common Unity Gotchas)

On Unity Answers we often see questions that say something like - when one of my enemy dies, they all die. This is normally caused by the developer using static variables because they couldn’t figure out how else to access the information that they were looking for. If this applies to you, read on!