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 davidwalmsley · Jan 22, 2013 at 04:41 AM · arraylisttargetting

Trouble with targeting enemies from a List

So I am following a tutorial on creating a simple combat system that is in C# and I am trying to convert it into unity script. I have the scene set up with a simple object that will be the player and three enemies. Each enemy is tagged "Enemy". I am trying to return their transforms into a List (later I'll change transform into something else, but I figured it was a good way to get it started). Here is the script Im using on the player object:

import System.Collections.Generic;

 public var targets : List.;
 public var go : GameObject[];

 
 function Start() {
     targets = new List.(); 
     
     AddAllEnemies();
 }
 
 function AddAllEnemies(){
     go = GameObject.FindGameObjectsWithTag("Enemy");
     
     for(var enemy : GameObject in go){
         AddTarget(enemy.Transform);
         
     }
 }
 function AddTarget(enemy : Transform){
     
     targets.Add(enemy);
 }
     


So when I go into play mode and inspect my variables, the three enemies are put into the go variable correctly, but when I check the targets variable it is returned with three objects that read None (Transform). Im not sure where Im running into the problem. Any help would be appreciated.

Thanks

edit: the List. both have < Transform > after them. Not sure why it isn't showing up in the code

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 AlucardJay · Jan 22, 2013 at 05:06 AM

when you pass enemy to the function AddTarget, you have a capitol T in Transform, this should be lowercase t for transform. This should have popped up as an error in the console : BCE0019: 'Transform' is not a member of 'UnityEngine.GameObject'. Did you mean 'transform'?

Also are you using import System.Collections.Generic; for your list ?

Apart from that, your script is fine =]

 #pragma strict
 import System.Collections.Generic;
 
 public var targets : List.<Transform>;
 public var go : GameObject[];
 
 function Start() 
 {
     targets = new List.<Transform>(); 
     
     AddAllEnemies();
 }
 
 function AddAllEnemies()
 {
     go = GameObject.FindGameObjectsWithTag("Enemy");
     
     for(var enemy : GameObject in go)
     {
         AddTarget(enemy.transform);
     }
 }
 
 function AddTarget(enemy : Transform)
 {
     targets.Add(enemy);
 }
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 davidwalmsley · Jan 22, 2013 at 02:08 PM 0
Share

That did the trick. What exactly is the difference between Transform and transform? I know theres a difference between gameObject and GameObject too, but I'm not quite I grasp it.

avatar image AlucardJay · Jan 22, 2013 at 02:23 PM 0
Share

I'm really not the person to answer that technical question, being a self-taught hobbyist! But my understanding is capitol Transform and GameObject refer to inbuilt classes (scripts within Unity that make everything work) and could also be called Components, and lowercase transform and gameObject are variables of a Component. That really doesn't hold because there is a special relationship between a gameObject and its transform, but for simplicity they are calling components or variables of that component.

Look at the API : http://docs.unity3d.com/Documentation/ScriptReference/GameObject.html

transform is a Variable of GameObject.

and with : http://docs.unity3d.com/Documentation/ScriptReference/Transform.html

gameObject is a Inherited Variable of Transform.

This is getting more confusing, so unless one of the pros can help out, it is easy just to think of capitol as a class which you are typecasting to , and lowercase is a variable of the component. Confused?! There is a special relationsip between A GameObject and a Transform, if you create an empty gameObject then it is both a GameObject, with a Transform component. They are best buddies, but that is going off-point to your question.

In your case, AddTarget(enemy.transform); is stating the transform variable of the GameObject enemy

avatar image AlucardJay · Jan 27, 2013 at 01:21 PM 0
Share

Hello. Did this answer work, or did you find the suggestion useful? Please post a comment or mark an answer, for future reference by other people searching this 'site.

avatar image davidwalmsley · Jan 28, 2013 at 05:22 AM 0
Share

Oh crap sorry, I had the thumbs up but I didn't check it. Sorry! I think I need to go back to all my other posts here and check the thumbs up on em...

avatar image AlucardJay · Jan 28, 2013 at 05:35 AM 0
Share

Thankyou, no worries. I had many answers without comments or feedback so last night I went through posting this everywhere. Am glad you got this working, thanks again, Happy Coding =]

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

10 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

Related Questions

How to find the opposite variables list? 1 Answer

instantiate problem help? 1 Answer

A node in a childnode? 1 Answer

Emptying a Generic List / Unexpected Behaviour 1 Answer

Find GameObjects with a certain Script [Solved] 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