- Home /
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.
$$anonymous$$aivo thanks for the steer in the right direction. Been thinking about it on and off today and I think this is the direction I need to try.
When I get home I'll try to change it up and perhaps add a collider to the player, and then from there start the correct code in the chop_tree script.
Now in regards to the trigger taking the GameObject as a parameter maybe this is where I'm going wrong on my tree collider - Do I need to ref the trigger - I mean do I need to do more then just set a condition for "Hey this trigger happened" - "Run this code for this tree" - wouldn't that still just change everything or the collider will already know that its only messing with the instance of the collider tree? Does unity have a small window to display a instance ID or anything of that nature? This would remove some questionable areas I've gotten myself into.
I guess its either one of two out comes... ;) Collider triggers and all trees attached to the script move - connected from the one collider still and it acts as the parent, or it works.
Answer by Kaivo · Jun 24, 2013 at 02:13 PM
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.
Answer by pengo · Jun 23, 2013 at 03:34 AM
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!
I'm not using any static variables in my scene at all. I understand how to ref from one script to another in the same object, and other objects - also to get child objects.
Its a nice site and I'll be looking at it more later.
$$anonymous$$aybe I'm not using Collision Enter right...
Perhaps I'm not using transform and component right to adjust JUST the tree I'm currently at. Have a few ideas I'm going to apply when I get home.
I do have a odd ball transform question also.
I have the player linked to tree in inspector, using a transform (public Transform treeTarget;)
I also have public Transform myTransform; which checks if the player is moving. It stores the current position, refs it to the last position and compares to see if the change in position has occurred if it has, use the correct animation.
I set the treeTarget in the inspector to the tree prefab I made. I set the myTransform to the player for the check (to check if moving). If I remove all the trees, then my character doesn't animate. Which is a bit odd because the other transform myTransform; should work correctly without a tree prefab on scene - but it throws a nul ref to myTransform ins$$anonymous$$d of treeTarget like I'd expect? The reason I'm stating this is because I also think it has something to do with ALL the trees getting hit and cut down. Perhaps I'm using transform incorrectly?
So I have TWO transforms in my player character, and if the tree is missing it nul refs my other transform for whatever reason...?
Either way the current way I check if the player is by the tree is rather clunky. There is a method in the tree script that's called using the players position in relation to the tree. I'm going to try to find a different method since this can be rather a good hit.
I would display the code here - but I'm not currently at home and will be updating when I can. I also added last night before calling it quits to the tree script to check if the player is within a certain distance to that specific tree - if hes not return. The other tree keeps standing once its cut - but if I move within a certain distance it falls over instantly. $$anonymous$$ild success I guess, but still the way I'm handling things needs to be re-thought.
HELP IS $$anonymous$$ORE THEN WELCO$$anonymous$$$$anonymous$$
Your answer
Follow this Question
Related Questions
make unique shader for prefab 1 Answer
Calling a function from all instances of a script 1 Answer
Script is reading from prefab instead of instance 1 Answer
Is it possible to have a prefab of a cube, which has instances with different colours? 2 Answers
How to have unique instances of script on multiple instances of prefab? 1 Answer