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 belindus · Jan 23, 2020 at 08:20 AM · instantiateruntimefindfindgameobjectswithtagfindwithtag

Can't figure out how to access an object after creation.,Can't figure out how to access a given instance of the object on mouse click.

Hello, I am spawning waves of enemies and when I click a mouse button I want to damage the given instance of enemy. It only works on the first one that is created, the next one is ignored. Tried finding it by name and by tag but didn't I failed. I just need a hint on how to figure it out.

 private IEnumerator SpawnDayWave()
         {
         Day day = days[dayCount];
 
         for (int i = 0; i < day.count; i++)
             {
             SpawnEnemy(day.enemy);
             yield return new WaitForSeconds(timeBetweenWaves);
             }
         dayCount++;
         }
 
     private void SpawnEnemy(GameObject enemy)
         {
         spawnPosition = new Vector3(transform.position.x, transform.position.y + 1, Random.Range(-3.3f, 3.3f));
         GameObject enemyAlive = Instantiate(enemy, spawnPosition, transform.rotation);
         enemyAlive.name = "Enemy1";
         enemiesAlive++;
         }

Here is my wavespawner and in the start method i start a coroutine.

 public class Player : MonoBehaviour
     {
     public float playerHealth;
     private float playerHealthStart = 100;
 
     public Enemy enemyScript;
     private float playerDamage = 30;
     private Ray ray;
     private RaycastHit hit;
 
     public string enemyTag = "Enemy";
 
     private float reloadTime = 2;
     private bool isReloaded = true;
 
     // Start is called before the first frame update
     private void Start()
         {
         SetStartHealth();
         enemyScript = GameObject.Find("Enemy1").GetComponent<Enemy>();
         }
 
     // Update is called once per frame
     private void Update()
         {
         if (playerHealth <= 0)
             {
             PlayerDied();
             }
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast(ray, out hit))
             {
             if (Input.GetMouseButtonDown(0) && isReloaded)
                 {
                 Shoot();
                 StartCoroutine(Reload());
                 }
             }
         }
 
     private void Shoot()
         {
         if (hit.collider.gameObject.CompareTag(enemyTag))
             {
             Debug.Log("Shooting!");
 
             enemyScript.EnemyHurt(playerDamage);
             isReloaded = false;
             if (enemyScript.enemyHealth <= 0)
                 {
                 enemyScript.EnemyDied();
                 }
             }
         else
             {
             isReloaded = false;
             }
         }
 
     private IEnumerator Reload()
         {
         yield return new WaitForSeconds(reloadTime);
         isReloaded = true;
         }
 
     public void HurtPlayer(float damage)
         {
         playerHealth -= damage;
         }
 
     public void SetStartHealth()
         {
         playerHealth = playerHealthStart;
         }
 
     private void PlayerDied()
         {
         Debug.Log("GameOver!");
         }
     }

And my player class where I get my mouse position and check if its the enemy. My idea also was to check for objectswithtag and everytime when I instantiate iterate over a list of enemies and add a new one. Although don't know if it would work or if my performance wouldn't suffer much.

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
0

Answer by sacredgeometry · Jan 23, 2020 at 09:28 PM

      enemyScript = GameObject.Find("Enemy1").GetComponent<Enemy>();

is grabbing the first enemy with that name that it finds and setting it in the enemyScript field.

Then you are calling

 enemyScript.EnemyHurt(playerDamage);

on it so yes. You are only ever going to attack one enemy and that enemy will be what ever enemy is currently set in that field.

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

145 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

Related Questions

How Expensive is Find function? 3 Answers

How to hit a specific object 2 Answers

What's the most efficient way of finding child objects at runtime? 1 Answer

Instantiating objects to create an infinite level. 1 Answer

find GUIText 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