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 /
This question was closed Jan 15, 2014 at 04:07 PM by Calum-McManus for the following reason:

Issue Sorted By Poster

avatar image
0
Question by Calum-McManus · Oct 03, 2013 at 12:19 PM · c#arraysontriggerenterliststower-defense

Add GameObjects to a list using OnTriggerEnter

Making a simple TD game but i'm having trouble with the order that the turrets i have shoot the targets. I want to add each targer to a list as each one moves into the attack radius of the turret but i'm not sure how i would do this. This is my code so far:

using UnityEngine; using System.Collections; using System.Collections.Generic; public class Turret : ClickToPlace { [SerializeField] private List<GameObject> m_aEnemys = new List<GameObject>(); [SerializeField] private GameObject m_SpikeSpawn; [SerializeField] private GameObject m_SpikePrefab; private EnemyMove m_Player; // Update is called once per frame void Update() { //If the player is near then the turret head follows the player if (m_Player != null) { Vector3 vDir = (m_Player.transform.position - transform.position).normalized; transform.forward = vDir; } } /// <summary> /// If the player is near start the Fire coroutine /// </summary> /// <param name="other">checks the Other object not its self</param> protected virtual void OnTriggerEnter(Collider other) { if (m_Player == null) { m_Player = other.GetComponent<EnemyMove>(); StartCoroutine(Fire()); } } /// <summary> /// If the player is not near then call the ResetRotation coroutine /// </summary> /// <param name="other">checks the Other object not its self</param> void OnTriggerExit(Collider other) { if (m_Player != null && m_Player.transform == other.transform) { m_Player = null; } } /// <summary> /// Tells the turret to return to its original possision slowly when called /// </summary> /// <returns></returns> private IEnumerator ResetRotation() { while (Quaternion.Angle(transform.localRotation, Quaternion.identity) > 0.1f) { transform.localRotation = Quaternion.Lerp(transform.localRotation, Quaternion.identity, Time.deltaTime * 10); yield return new WaitForEndOfFrame(); } } /// <summary> /// Tells the turret to wait one second then start firing a projectile in a straight line from the barrel of the turret /// </summary> /// <returns></returns> private IEnumerator Fire() { while (m_Player != null) { yield return new WaitForSeconds(m_fFireRate); if (m_Player != null) { Instantiate(m_SpikePrefab, m_SpikeSpawn.transform.position, m_SpikeSpawn.transform.rotation); } } } } All help is appreciated :')

Comment
Add comment · Show 6
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 ShadoX · Oct 03, 2013 at 12:48 PM 1
Share
  protected virtual void OnTriggerEnter(Collider other)
     {
       m_aEnemys.add(other.gameObject);
      //.... 
     }
avatar image Calum-McManus · Oct 03, 2013 at 12:50 PM 0
Share

It keeps telling me that it cant resolve symbol add? Edit $$anonymous$$y bad, the A had to capitalised, that worked thanks!, while this is still open is there a way now remove them from the list when they leave the area or are destroyed?

avatar image KiraSensei · Oct 03, 2013 at 01:39 PM 0
Share

You can use OnTriggerExit() method to know when it exists the area, but I don't know how you can be notified of the destruction of the object. You can use the Remove method on the list to remove an element.

avatar image ShadoX · Oct 03, 2013 at 01:54 PM 0
Share

What $$anonymous$$iraSensei said ^ You might want to take a look at http://www.dotnetperls.com/list

avatar image KiraSensei · Oct 03, 2013 at 01:57 PM 0
Share

$$anonymous$$aybe before using the game object, you can check with GameObject.Find(...) if it still exists.

Show more comments

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Serialization Error 1 Answer

Lists, Arrays and c# equivilent of js Array() 1 Answer

creating and manipulating a grid efficiently 1 Answer

What is the maximum amount of objects that can be stored in an Array or List without causing any error? 1 Answer

How do i convert this code snippet to a list? 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