- Home /
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);
}
}
}
Answer by TachyonBlast · Aug 30, 2018 at 07:17 PM
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.
hi thank you so much for your reply.
(position, rotation)produce an error,
i tried Instantiate(object).GetComponent(); but it does not work
Is it possible that it is because im in 2D?
object, position and rotation are just placeholder names for your variables. Object should be the GameObject you want to Instantiate, position should be a Vector3 which contains the position where your object should spawn (as you are in 2D create a new Vector3 and set z value to 0) and finally you probably want to change rotation to Quaternion.identity
Thank you again for your reply. However, this method will only work work for the first instantiated object. Is there a way for me to keep it working for each time the object instantiated ?
Truly appreciate your reply.
Your answer

Follow this Question
Related Questions
Deep clone a gameobject 2 Answers
Problem with finding animator. 1 Answer
Instantiate creating infinite clones 0 Answers
How can I assign a clone with a script to a variable? without drag & drop 1 Answer
my object instantiates to much 1 Answer