Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 bryansull · Feb 12, 2015 at 08:29 PM · animatorscript errorclone

anim.SetBool not working on cloned GameObjects

Hey, new to Unity2D. I currently working on creating a 2D sidescroller game. I am cloning an enemy in my game that when it take a hit it should set a boolean to true. The issue is that it will change the expression on the original object but not the the clone. Below is the script attached to the original object.

using UnityEngine; using System.Collections;

public class EnemyScript : MonoBehaviour {

 public Transform sightStart, sightEnd, target;
 public bool spotted=false;
 public bool facingLeft= true;
 public HealthScript character;
 public ScoreScript characterA;
 public Transform zombie;
 public float velocity=2f;
 RaycastHit2D WhatIHit;
 public bool InRange;
 Animator anim;
 int EcurrentHealth =2;

 void Start(){

     anim = GetComponent<Animator> ();
                     
 }

 // Update is called once per frame
 void FixedUpdate () {

     Raycasting ();

         
 }

 void Raycasting(){
             spotted = Physics2D.Linecast (sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer ("Player"));
             
     if (spotted == true ) {
                     anim.SetBool ("Spotted",true);
             } else {
         anim.SetBool ("Spotted",false);
             }
             facingLeft = !facingLeft;
      
                     if (target.position.x < zombie.position.x) {
                             transform.eulerAngles = new Vector2 (0, 180);
                             rigidbody2D.velocity = new Vector2 (velocity * -1, rigidbody2D.velocity.y);// enemy is walks to the left
                     } else {
                             transform.eulerAngles = new Vector2 (0, 0);
                             rigidbody2D.velocity = new Vector2 (velocity, rigidbody2D.velocity.y);// enemy moving to the right
                     }
             
     if (Physics2D.Linecast (sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer ("Player"))) {
                     WhatIHit = Physics2D.Linecast (sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer ("Player"));
                     InRange = true;
             }
     else 
     {
         WhatIHit = Physics2D.Linecast (sightStart.position, sightEnd.position, 1 << LayerMask.NameToLayer ("Player"));
         InRange=false;
     }

 }

 void AttackPlayer(){
     StartCoroutine (WaitSet ());
     if (InRange == true) 
     {
         character.ApplyDamage(1);
     }
     }
 IEnumerator WaitSet(){
             yield return new WaitForSeconds (1);
     }
     
 public void EnemyDamage (int enemyhit){
     anim.SetBool ("IwasHit",true);
     EcurrentHealth = EcurrentHealth - enemyhit;
             if (EcurrentHealth <= 0) {
         Destroy(GameObject.Find("ZombieA(Clone)"));
                 }
     }

}

Comment
Add comment · Show 1
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 chintan_shroff · Feb 13, 2015 at 02:06 AM 0
Share

please post the code for how you are instantiating the clones for the game object

2 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by chintan_shroff · Feb 13, 2015 at 06:01 PM

Instead of using StartCoroutine you could use InvokeRepeating

  void Start()
  {
  // So the EnemySpawn function is called after 3secs after which a new enemy is generated every 2 secs.
  InvokeRepeating ("EnemySpawn",3,2F);   
  }


Instantiate new clones as Rigidbody2D.

  void EnemySpawn()
 {
     Rigidbody2D enemyInstance;
     enemyInstance = Instantiate(enemy, transform.position, Quaternion.identity) as Rigidbody2D;
 }

Now you can reference these clones as gameObject.name = "Enemy(Clones)".Hope this helps!

Comment
Add comment · Show 2 · 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 InvincibleCat · Feb 13, 2015 at 06:19 PM 0
Share

Did you try your code ?

avatar image chintan_shroff · Feb 13, 2015 at 06:39 PM 0
Share

yes, this is how I have been instantiating clones in my games. Considering the fact that I am very new myself, I believe there might be a better way to do this but the way I have implemented the code is also correct according to me. Would really help if you could correct me here.

avatar image
0

Answer by bryansull · Feb 13, 2015 at 02:26 PM

This is my script for cloning, its attached to an empty game object in my scene

using UnityEngine; using System.Collections;

public class SpawnManager : MonoBehaviour { public GameObject enemy;

 // Use this for initialization
 void Start () {
     StartCoroutine(EnemySpawn());
         }

 IEnumerator EnemySpawn(){
     while (true) {
         Instantiate(enemy, transform.position, Quaternion.identity);
         yield return new WaitForSeconds(4f);
     }
 }


}

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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

GetComponent Reference are lost after use Animator 0 Answers

2D Animation does not start 1 Answer

NullReferenceException error 0 Answers

Prefab script on clones only reference the first clone instantiated 1 Answer

Updating animator for instantiated/cloned object 2 Answers


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