Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ikmal-Rani · Aug 29, 2018 at 04:18 PM · instantiateanimatorclone

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);
             }
         }
     
     }
     
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Ikmal-Rani · Aug 30, 2018 at 02:28 PM

Hi, any help? :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ikmal-Rani · Aug 31, 2018 at 01:06 PM 0
Share

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?

avatar image TachyonBlast · Aug 31, 2018 at 02:07 PM 0
Share

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

avatar image Ikmal-Rani TachyonBlast · Sep 01, 2018 at 11:15 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

122 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges