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 TheGeneralBenLee · Jul 18, 2021 at 03:07 PM · gameobjectenumlengthstateschecking

How do i check enum state from a length of found GameObjects?

For Context, I have a game where you can build settlements, and there is a script I have called SettlementBrain, which takes information from a static, non monobehaviour serializable class called SettlementStatistics, then the information that is processed gets sent to the GUI script for the settlement. Settler gameobjects have a tag called castaway, and the SettlementBrain counts these no problem to determine the amount of settlers. Here's the problem: the settlers each have enums, which are on a non monobehaviour serializable class, to determine if they have settled or if they're waiting for the player to find them and welcome them into the settlement. so the options for the enum are: castaway, settled, leftsettlement. The settlementBrain should, for each castaway tag they count, they should check the state of the enum. In theory, the brain will count how many settlers there are, and ignore the ones who are not settled. I hope this makes sense. Here is the code for the check settlement population function so far. I have only put in what i have so far. Thank you in advance to the community.

public void checkPopulation() { float Castaways = GameObject.FindGameObjectsWithTag("Castaway").Length; Debug.Log(Castaways); }

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
0
Best Answer

Answer by tyruji · Jul 22, 2021 at 01:11 PM

It's really hard to tell what you have in mind, especially when you posted just 2 lines of code :\, but if I understood correctly you want to count how many "castaways" have the state "settled". example code:

 public void CheckPopulation()
 {
         int count = 0;
         foreach( var castaway in FindGameObjectsWithTag("Castaway") )
         {
                 if( castaway.settlementState != SettlementState.settled ) continue;
                 ++count;
         }
         // do stuff ...
 }

And then you'll be left with the count of settled "castaways". Hope it helped.

Comment
Add comment · Show 2 · 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 TheGeneralBenLee · Jul 22, 2021 at 08:34 PM 0
Share

unfortunately this didnt work, I got an error saying " 'GameObject' does not contain a definition for 'SettlerSettleState' and no accessible extension method 'SettlerSettleState' accepting a first argument of type 'GameObject' could be found (are you missing a using directive or an assembly reference?) [Assembly-CSharp]csharp(CS1061) "

here is the code I inputted and i have included a bit more info, I apologise for not being clearer to start with. The error i have tried to highlight in bold is on the brain script if it does not show

here is what is in the settlementBrain script:

 public void checkPopulation()
     {
         int count = 0;
         foreach (var castaway in GameObject.FindGameObjectsWithTag("Castaway"))
         {
             if (castaway.***SettlerSettleState*** != SettlerSettleState.settled)
             {
                 continue;
                 count++;
             }
         }

here is the settler properties script, with the necessary info only

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable]
 public class SettlerProperties
 {
     public SettlerSettleState settleState;
     public SettlerAttackState sAttackState;
 }
 public enum SettlerSettleState 
 {
     estranged,
     settled,
     departed
 }

the class above is accessed with no problems by this class, which is attached to said game object(s) with the tag "Castaway"

 public class Settler : MonoBehaviour
 {
     public SettlerProperties sProperties;
 }

I hope this is enough info. If not please let me know, I am keen to learn and be better at asking questions to this community.

avatar image tyruji · Jul 25, 2021 at 06:20 PM 0
Share

Oh, no sorry the problem lies on my behalf - "FindGameObjectsWithTag( string )" returns a GameObject (not your castaway script!), here's how you'd use it with your given classes:

          foreach( var castaway in FindGameObjectsWithTag("Castaway") )
          {
                   //here you need to get the "Settler" component
                   Settler settler = castaway.GetComponent< Settler >();
                   if( settler == null ) continue; // if it didn't have this component
                                                                     // then just skip it.
                   var state = settler.sProperties.settleState;
                  if( state != SettlerSettleState.settled ) continue;
                  ++count;
          }

Good luck!

avatar image
0

Answer by TheGeneralBenLee · Jul 27, 2021 at 05:12 PM

Thanks for your help! I managed to figure this out eventually just before your answer. All I missed out was the if == null part. Thank you tho! Your answer has given me a lot cleaner code than my result! :)

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

186 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 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

Enum and Variable with similar name resulting in error 1 Answer

NPC Class looking for help 1 Answer

Update not behaving like it should with enum states 1 Answer

How do I find the size of a gameObject? 3 Answers

Random.Range, GameObjects and Length 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