Search Unity

Animator prefabs

Discussion in 'Animation' started by RPGcakes, Dec 21, 2018.

  1. RPGcakes

    RPGcakes

    Joined:
    Nov 6, 2018
    Posts:
    22
    So I've got a script that cuts down trees and then after a timer instantiates a new tree (to refresh the loot script mostly)

    This works fine on one tree only

    The tree is just a tree until 0 'tree hp' and then at 0 it turns into a tree stump where it is then lootable so I had to position the tree stump separately to match the trees location.

    The issue is that if I drag another tree prefab into the hierarchy, the tree and the tree stump both go to the original location of tree 1

    I've tried parenting tree 2 to an empty game object which allows me to move the tree and set up the positions again but once the tree is instantiating it's going back to tree 1s position

    I also tried setting the animations individually in the animator but have the same issue with positioning

    Any ideas?
     
  2. knobblez

    knobblez

    Joined:
    Nov 26, 2017
    Posts:
    223
    I suggest you show some code.

    So it sounds like the way you are referencing the tree is by declaring it public and drag-and-dropping the tree prefab into the script. You need to make use of referencing the GO that the script is attached to. That way the script reacts the same way no matter what tree it is attached to.

    instead of this:

    tree.getcomponent<SCRIPTNAME>().dostuff

    Using GO.getcomponent is specific and so you have to be specific for each tree.

    do this:

    this.getcomponent<SCRIPTNAME>().dostuff

    Using this.getcomponent or just getcomponent tells this script to grab the component from whatever GO this script is attached to, which allows it to have universal tree code.