Updating animator for instantiated/cloned object

Hi guys, the cloned object is not playing the “highlighted” animation.

The initial object does play the animation, but once it cloned on the scene, the animation is not playing.

I try attaching the duplicate code to an empty and parenting this one to it. I also try making the object a child to the empty, It does not work.

I have two codes, one attached to the object that will collide with the main OBJECT and will change the animation. Another is a duplicate/clone script attached to the camera. The animation does work on the original object but not on the instantiated/cloned object.

Would really appreciate your assistance on this one.

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

public class DetectObjectonCollision : MonoBehaviour {

    public Animator objectAnimator;
    public GameObject Object;

    void Start()
    {
        Object = GameObject.FindWithTag("object");
        objectAnimator = object.GetComponent<Animator>();
    }

    // Use this for initialization
    void OnCollisionEnter2D() {

        Debug.Log("hit");
        ScoreScript.scoreValue += 1;
        objectAnimator.SetFloat("Collide", 1);
        Debug.Log(ScoreScript.scoreValue);

    }

    void OnCollisionExit2D()
    {
        objectAnimator.SetFloat("Collide", 0);
    }
}

Duplicate script

 using UnityEngine;
    using System.Collections;
    
    public class DuplicateObject : MonoBehaviour { 
    	
    	public GameObject prefab;
        public float minSpawnTime = 1.0f;
    	public float maxSpawnTime = 3.0f;
    	public float distFromCamera = 10.0f;
    
    	private float timer = 0.0f;
    	private float nextTime;
    
    	void Start () {
    		nextTime = Random.Range(minSpawnTime, maxSpawnTime);
    			}
    
    
    	void Update () {
    		timer += Time.deltaTime;
    
    		prefab = GameObject.FindWithTag("Object");
    
    		if (timer > nextTime) {
    			Vector3 pos = new Vector3(Random.value, Random.value, distFromCamera);
    			pos = Camera.main.ViewportToWorldPoint(pos);
    
    			Instantiate(prefab, pos, Quaternion.identity);
    
    			Debug.Log("Object created");
    
    			timer = 0.0f;
    			nextTime = Random.Range(minSpawnTime, maxSpawnTime);
    		}
    	}
    
    }

Hi, any help? :slight_smile:

I’m not sure if this is what you want, but you can get the Animator of the cloned object from a script attached to the clone using

Animator anim = gameObject.GetComponent<Animator>();

or directly after instantiating the object, from the script that clones it:

Animator anim = Instantiate(object, position, rotation).GetComponent<Animator>();

make sure you don’t overwrite the value of this variable before setting the parameters of the animator.