Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Biticalifi · Feb 22 at 10:47 AM · instantiateunity 2dvariableclones

How do I check the variable of a specific clone?

I am trying to make a game where multiple enemies come down to attack. I Instantiate them and have them randomly come down. But when I check if they are touching a bullet, only the last instantiated enemy can check it.

Here is the code I am using: (The last 2 if statements are supposed to work for all clones but only works for the last instantiated one)

 public ButtonManager script;

 public GameObject enemy1;
 float SpawnTime1 = 0;
 public float MaxTime1;
 public Enemy1AI script1;
 GameObject newEnemy1;
 static Enemy1AI staticVar;

 void Update()
 {
     if(script.playingGame == true)
     {
         SpawnTime1 += Time.deltaTime;
         if (SpawnTime1 > MaxTime1)
         {
             newEnemy1 = Instantiate(enemy1);
             newEnemy1.transform.position = new Vector2(Random.Range(-4.8f, 4.8f), 10f);
             SpawnTime1 = 0;
             MaxTime1 = Random.Range(4, 5);
         }
         Enemy1AI staticVar = newEnemy1.gameObject.GetComponent<Enemy1AI>();
         if (staticVar != null)
         {
             if (staticVar.transform.position.y < -7)
             {
                 Destroy(newEnemy1);
                 script.health -= 1;
             }
             if (staticVar.isTouchingBullet == true)
             {
                 Destroy(newEnemy1);
                 script.sc += 100;
             }
         }
     }
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Caeser_21 · Feb 22 at 12:13 PM

This is happening because you are only checking the script of the last spawned enemy. To counteract this do the following...
1. Create a new tag called "Enemy1" and add it to the 'enemy1' Gameobject.
2. Then change the script to the following...

  public ButtonManager script;
  public GameObject enemy1;
  float SpawnTime1 = 0;
  public float MaxTime1;
  public Enemy1AI script1;
  GameObject newEnemy1;
  static Enemy1AI staticVar;
  private GameObject[] 1Enemies;
  void Update()
  {
      Enemies = GameObject.FindGameObjectsWithTag("Enemy1")
 
      if(script.playingGame == true)
      {
          SpawnTime1 += Time.deltaTime;
          if (SpawnTime1 > MaxTime1)
          {
              newEnemy1 = Instantiate(enemy1);
              newEnemy1.transform.position = new Vector2(Random.Range(-4.8f, 4.8f), 10f);
              SpawnTime1 = 0;
              MaxTime1 = Random.Range(4, 5);
          }
          
          foreach (GameObject Enemy in 1Enemies)
          {
               Enemy1AI staticVar = Enemy.gameObject.GetComponent<Enemy1AI>();
               if (staticVar != null)
               {
                   if (staticVar.transform.position.y < -7)
                   {
                       Destroy(Enemy);
                       script.health -= 1;
                   }
                   if (staticVar.isTouchingBullet == true)
                   {
                       Destroy(Enemy);
                       script.sc += 100;
                   }
               }
          }
      }
  }

NOTE : I have no clue what the 'ButtonManager' script does so I don't know whether to change it or not

Comment
Add comment · Show 1 · 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 Biticalifi · Feb 22 at 05:52 PM 0
Share

Thank you so much! I was stuck on this problem for about an hour, I am very thankful of you.

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

170 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 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

Why is instantiate and playerprefs not working when combined? 1 Answer

How to carry over variable (transform.position) and use it as Vector3 for instanitating a prefab 2 Answers

How can I assign a clone with a script to a variable? without drag & drop 1 Answer

setting the text of an instantiated prefab's child's guitext object 1 Answer

Unity is automatically instantiating variables 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