- Home /
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); }
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.
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.
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!
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! :)