How to set proper animation to prefabs randomly instantiated?

Hello. My script is spawning a random ninja between 4 ninja prefabs.
Every ninja shares the same animatorcontroller. Although I try to call each instantiated ninja’s animator, the animation just won’t run. More spcifically, the parameter I’m trying to change does not change in the condition I set it to change.
What’s the best way to do this?
As I said, I followed these instructions: c# - Duplicated Animations between prefabs - Stack Overflow, but it did nothing. I’m a total noob at this. This is my first game and I’m stuck at this point. I’d appreciate some help.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spawner : MonoBehaviour
{
    [SerializeField]
    public GameObject ninjal;
    public GameObject ninjar;
    public GameObject ninjau;
    public GameObject ninjad;
    public float timer = 0;
    public float maxtime = 10;   
    public GameObject[] ninjas;
    private int randomprefab;
    Animator anim;
    public GameObject newninja;
    

    void Start()
    {
        anim = GetComponent<Animator>();
        var ninjal = Resources.Load("NinjaNPCleft Variant") as GameObject;
        var ninjar = Resources.Load("NinjaNPCright Variant") as GameObject;
        var ninjau = Resources.Load("NinjaNPCup Variant") as GameObject;
        var ninjad = Resources.Load("NinjaNPCdown Variant") as GameObject;


        float randomnumber = Random.Range(0, 4);
        switch (randomnumber)
        {
            case 0: GameObject.Instantiate(ninjal, ninjal.transform.position, ninjal.transform.rotation); break;
            case 1: GameObject.Instantiate(ninjar, ninjar.transform.position, ninjar.transform.rotation); break;
            case 2: GameObject.Instantiate(ninjau, ninjau.transform.position, ninjau.transform.rotation); break;
            case 3: GameObject.Instantiate(ninjad, ninjad.transform.position, ninjad.transform.rotation); break;
        }

        
    }

    // Update is called once per frame
    void Update()
    {
        if (timer > maxtime)
        {
            randomprefab = Random.Range(0, 4);
            newninja = Instantiate(ninjas[randomprefab], ninjas[randomprefab].transform.position, ninjas[randomprefab].transform.rotation);
            timer = 0;
            
        }

        timer += Time.deltaTime;

        if (newninja.transform.position.x == 7.2f)
        {
           newninja.GetComponent<Animator>().SetInteger("attack", 1);
        }



    }
}

On awake I don’t want the ninja spawned to change to attack. But I want the rest of them (those instantiated every 10 seconds) to do so on certain conditions without it influencing the animation of any other ninja that does not meet the condition.
I appreciate the patience and sorry if the code is rustic or has any none sense that you may find “insulting” to even show online.
Thank you very much.

First of all, if the “newninja” is not altered by another script, then for the first 10 seconds after Start(), it’s value is null.Here :

if (newninja.transform.position.x == 7.2f)
         {
            newninja.GetComponent<Animator>().SetInteger("attack", 1);
         }

you are trying to perform actions on null object, so it should throw an “NullReferenceException”.
To fix that, change your Update() method as follows :

void Update()
{
    if (timer > maxtime)
    {
        randomprefab = Random.Range(0, 4);
        newninja = Instantiate(ninjas[randomprefab], ninjas[randomprefab].transform.position, ninjas[randomprefab].transform.rotation);
        timer = 0;

        if (newninja.transform.position.x == 7.2f)
        {
            newninja.GetComponent<Animator>().SetInteger("attack", 1);
        }
    }
    timer += Time.deltaTime;

}

Why if (newninja.transform.position.x == 7.2f)?

Disclaimer :

Naming conventions! (xd)

If you don’t do things with them

     public GameObject ninjal;
     public GameObject ninjar;
     public GameObject ninjau;
     public GameObject ninjad;

in your other scripts, then it’s better to keep them as private.
You should make them into array.


var ninjal = Resources.Load("NinjaNPCleft Variant") as GameObject;
var ninjar = Resources.Load("NinjaNPCright Variant") as GameObject;
var ninjau = Resources.Load("NinjaNPCup Variant") as GameObject;
var ninjad = Resources.Load("NinjaNPCdown Variant") as GameObject;

That “var” keyword is as far as I know unnecessary and confusing, as it “looks” like you’re creating new variables, but in reality you are not.

You should consider putting your code from Update() to a coroutine.