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
1
Question by Theyrr · Oct 21, 2013 at 06:37 PM · listenemyattackindextower defense

Get First Object In List

I am building an tower defense. I have a list, that fills when an enemy enters the turrent's range. But how can I get the first object in the list and then assing it to be the target for the turret?

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Cannon : MonoBehaviour {
     public GameObject projectile;
     public float reloadTime = 1f;
     public float rotateSpeed = 5f;
     public float cooldown = .25f;
     public GameObject muzzleEffect;
     public float errorAmount = .001f;
     public Transform target;
     public Transform[] muzzlePositions;
     public Transform turretBall;
     public GameObject aimHere;
     public List<GameObject> targetList = new List<GameObject>();
     
     private float nextFireTime;
     private float nextMoveTime;
     private Quaternion desiredRotation;
     private float aimError;
 
     void Start() {
 
     }
 
     void Update (){
         if(target) {
             if(Time.time >= nextMoveTime) {
                 CalculateAimPosition(target.position);
                 turretBall.rotation = Quaternion.Lerp(turretBall.rotation, desiredRotation, Time.deltaTime * rotateSpeed);
             }
             
             if(Time.time >= nextFireTime) {
                 FireProjectile();
             }
         }
     }
     
     void  OnTriggerEnter (Collider other){
         if(other.gameObject.tag == "AimPoint") {
 //            nextFireTime = Time.time + (reloadTime * .5f);
 //            target = other.gameObject.transform;
             targetList.Add(other.gameObject);
         }
     }
     
     void  OnTriggerExit (Collider other){
         if(other.gameObject.tag == "AimPoint") {
             targetList.Remove(other.gameObject);
         }
     }
     
     void  CalculateAimPosition (Vector3 targetPos){
         //Vector3 aimPoint = new Vector3(targetPos.x + aimError, targetPos.y + aimError, targetPos.z + aimError);
         Vector3 aimPoint = new Vector3(aimHere.transform.position.x, aimHere.transform.position.y, aimHere.transform.position.z);
         if (aimPoint != turretBall.position) {
             desiredRotation = Quaternion.LookRotation(turretBall.position - aimPoint);
         }
     }
     
     void  CalculateAimError (){
         aimError = Random.Range(-errorAmount, errorAmount);
     }
     
     void  FireProjectile (){
         //audio.Play();
         nextFireTime = Time.time + reloadTime;
         nextMoveTime = Time.time + cooldown;
         CalculateAimError();
         
         foreach(Transform theMuzzlePos in muzzlePositions) {
             Instantiate(projectile, theMuzzlePos.position, theMuzzlePos.rotation);
             //Instantiate(muzzleEffect, theMuzzlePos.position, theMuzzlePos.rotation);
         }
     }
 }
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
8
Best Answer

Answer by mattssonon · Oct 21, 2013 at 07:57 PM

Like an array, targetList[0].

Comment
Add comment · Show 5 · 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 Diesign · Jul 20, 2017 at 08:30 PM 0
Share

@mattssonon How would you use an array to select the first object in the list? Or are you suggesting we use an array ins$$anonymous$$d of a gameobject list?

Is it possible to just select the first object from the gameobject list ins$$anonymous$$d of using an array?

avatar image jmgek Diesign · Jul 20, 2017 at 08:52 PM 1
Share

A list is just a collection 'essentially an array' that contains pointers to a head and a tail node and class methods to search, sort, insert, etc.

targetList[0] is the first element in the list...

https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx

avatar image Bunny83 jmgek · Jul 20, 2017 at 09:27 PM 1
Share

Note that a generic List<T> is not a linked list. Internally a List is using an actual array. However arrays can't be resized. A List uses an array which is automatically replaced when it runs out of capacity. So a List is just a wrapper for a native array and allows easy adding and removing of items. There is no head or tail pointer. A List just independently tracks the number of elements stored which allows the list to use a larger array to have reserve.

Show more comments
Show more comments
avatar image
0

Answer by Eragon1158 · Jul 20, 2017 at 09:38 PM

Don't know if this helps but Brackeys has a great tower defense game tutorial. Here is the link,

https://www.youtube.com/watch?v=beuoNuK2tbk

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 Bunny83 · Jul 20, 2017 at 10:22 PM 0
Share

This is actually off-topic. The question was about how to get the first element in a List. This answer doesn't answer the question. It's way too general as an answer.

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

19 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

Related Questions

A node in a childnode? 1 Answer

Show only selected enemy's health bar 1 Answer

How do I get my enemy to attack? 0 Answers

Can anyone help with me with my attack script and enemy script? 1 Answer

Melee range AI 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