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 adlej1 · Nov 20, 2014 at 08:16 PM · arrayvariableclassdeclaration

Declaring Variables in an Array of a Class

I want to be able to set the variables in an array of a custom class, like so:

 //Class of an enemy in the level, and whether they are in range or sight
 public class Enemy {
 
     public GameObject enemy;
 
     public bool inRange;
     public bool inSight;
 }
 
 //Array of enemies
 public Enemy[] enemies;
 
 //Find all enemy units in the level
 void Awake () {
 
     enemies.enemy = GameObject.FindGameObjectsWithTag ("Enemy");
 
     enemies.inRange = false;
     enemies.inSight = false;
 }

Even if this is an invalid way to do this sort of thing, is there any way I can?

Comment
Add comment · Show 4
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 richyrich · Nov 22, 2014 at 01:45 AM 0
Share

It would make more sense if the array was of GameObjects and that the Enemy class inherited from $$anonymous$$onobehaviour and was made a component of the GameObject, rather than having the GameObject an attribute of the Enemy.

So something like this

Warning code may not compile, not tested, just an idea

 //Class of an enemy in the level, and whether they are in range or sight
 public class Enemy : $$anonymous$$onoBehaviour {
     //public GameObject enemy;
     public bool inRange;
     public bool inSight;
 }
 
     //Array of enemies
     //public Enemy[] enemies;
     public GameObject[] enemies;
      
      //Find all enemy units in the level - better to deal with other objects after they have been initialized (Start)
 
     void Start () {
         enemies = GameObject.FindGameObjectsWithTag ("Enemy");
         for each (GameObject go in enemies)
         {
             Enemy enmy = go.GetComponent<"Enemy">();
             enmy.inRange = false;
             enmy.inSight = false;
         }
     }

The above assumes you have used a tag called "Enemy" as well as having a class called Enemy

avatar image Uldeim · Nov 22, 2014 at 01:58 AM 0
Share

Not sure I agree; composition over inheritance is a good thing. Note that GetComponent will only work if Enemy both inherits from $$anonymous$$onoBehaviour (as it does here) AND is present on every Enemy.

avatar image NoseKills · Nov 22, 2014 at 02:00 AM 1
Share

Small correction to @richyrich 's good answer

i believe it's either

 Enemy enmy = go.GetComponent<Enemy>();
 // or 
 Enemy enmy = go.GetComponent("Enemy");
avatar image richyrich · Nov 22, 2014 at 02:27 AM 0
Share

@Nose$$anonymous$$ills - well spotted. I've been dabbling in UnityScript today and got myself confused ;) For c# it should be first one

@Uldeim - when commenting/answering a question, I try to use mainly the OP's code, otherwise it serves little point. In the OP code, we have FindGameObjectsWithTag - now that returns GameObjects, not the custom class as was desired. The OP wanted to then edit the attributes. It therefore suggests that if the objects are GameObjects which have the same tag, they all have a shared behaviour. On that premise, it is fair to say they share at least one same script in order to share the behaviour. Removing the GameObject attribute simplifies code readability and the general concepts I was trying to convey. I made some assumptions and put forward a suggestion on that basis - hence a comment not an answer

However, given the OP stated that he wanted to know a way, even if 'invalid' we can all be right :D

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Creating A Class Variable That Works With Any Class 0 Answers

Declaring a variable in Start funciton 3 Answers

Accesing custom class object from another script 1 Answer

How do I change public variables depending on which level? 1 Answer

Possible to use an array value as the name of a variable? 2 Answers


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