0
votes

I am instantiating game objects by a For loop:

Dragon Manager script:

 //Instantiate prefab
  enemyDragons2[i] = (GameObject)Instantiate(enemyDragonStandIn,
  PlaceToSpawn - Vector3.forward * 20.4f, Quaternion.identity);

  //Attach script
  enemyDragons2[i].AddComponent<SeekChase>();

In the SeekChase script, I have at the beginning:

  static Animator anim;

and in the Start I have:

  anim GetComponent<Animator>();

In the Unity editor I have everything set up: State Machine ,Parameter, Transitions and Conditions, to play from one animation, and then to another when a condition is met.

I thought that each clone game object would have the static Animator and the anim GetComponents() parts attached, but I have continuously received the error: "No Animator attached to the Clone game Object, but a script is trying to access it". This points to ,(place of the error(s):

anim.SetBool ("isIdol",false);
anim.SetBool ("isTurning",true);

I also tried to add to SeekChase:

 **public**static Animator anim; (though it dosent seem like I should have to do this.)

In the Dragon Manager script( where the objects are instantiated) I tried adding the line:

  static Animator anim; (in the Start)

and

  anim = enemyDragons[i].AddComponent<Animator>();

I recieved the error: "animator is not playing an animator controller"

But animations don't seem to play through code, just the default take, but not transition to the next("isTurning").It seems like I am missing something simple.

Any help will be appreciated.

Thank You.

============================================ EDIT/Solution

To begin. Unity should not let you continue if there is a level error, because it gives you a false sense of being about the functioning of things.

When I was testing, I was using ONE prefab, (of 6), to test animations on. They all were using the SeekChase script. So when I would press Play, I would always get the errors:

  "No Animator attached to the Clone game Object, but a script is trying to 
    access it".

I sort of expected an error because the other instantiated objects were having their Animators accessed by the SeekChase script, though they did not have one. Since I was testing on one object that was being instantiated, and Unity allowed me to "Play", I did not think that that error was related to the functioning of my current animation problem.

With your suggestions, I was able to, in the State Machine, with transitions arrows attached, have the two animations play from one to the other ( this wasn't being done before). But, with the condition that I had to set the bool:

       if(direction.magnitude < 10.0f) 

  {

                // Animator Stuff####
                anim.SetBool("isIdol",true);
                anim.SetBool("isTurning",true);...

  }


   ..... the second animation was not being played.

Then I thought about the persistent error, and decided to comment out SeekChase script being applied to the other game objects......and then the second animation it played! Hopefully when I fix all the prefabs and insatiate them, all will function.

Thanks again.

1

1 Answers

0
votes

First make enemyDragonStandIn as gameobject without instantiating with the animations working, then make that gameobject prefab, after that you can instantiate it with the rigth animations instead of adding them on runing time. Finally call on the instantiated object

     anim.SetBool ("isTurning",true)