0

So i was trying to put all the attacks in a single list of GameObjects. They all have different tags (i use tags to know where to move them, probably not the best approach but it was the best i could find)

I tried Doing a FindGameObjectsWithTag() for all tags and concatenating them together but it gave the

error CS0176: Member 'string.Concat(object, object, object)' cannot be accessed with an instance reference; qualify it with a type name instead

Then i tried calling the members with the tag (so "Directions/Up" instead of EnemyDirections[0]) but same error

Here's the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;


public enum BattleState
{
    Start,
    PlayerTurn,
    EnemyTurn,
    FinishedTurn,
    Won,
    Lost
  
}
    
public class TurnHandler : MonoBehaviour
{
    List<string> EnemyDirections;
    //Player
        //Scripts
    PlayerHealth PlayerHp;
    [SerializeField] PlayerMovement PlayerMove;
        //Objects
    [SerializeField]GameObject PlayerUI;
    GameObject Player;
    //Battlestate
    public BattleState state;
    private bool enemyActed;
    //AttacksGoingUp
    private GameObject[] EnemyAtcks0;
    //AttacksGoingDown
    private GameObject[] EnemyAtcks1;
    //AttacksGoingLeft
    private GameObject[] EnemyAtcks2;
    //AttacksGoingRight
    private GameObject[] EnemyAtcks3;
    //AllAtacks
    private GameObject[] EnemyAtcks;
    
    [SerializeField] int Enemies = 1;

    
   public void PlayerAct ()
   {
        playerfinishTurn();
   }
    
   void playerfinishTurn()
   {
        Player.SetActive(false);

        state = BattleState.EnemyTurn;
   }

   void EnemyFinishedTurn()
   {
        foreach (GameObject obj in EnemyAtcks)
        {
            Destroy(obj);
        }
        enemyActed = false;
        state = BattleState.FinishedTurn;
   }

    void Start()
    {
        EnemySetDirections();
        //Player
        Player = GameObject.FindGameObjectWithTag("Player");
        //BattleState
        state = BattleState.Start;
        enemyActed = false;
    }
    
        
    


    void Update()
    {
        if (state == BattleState.Start)
        {
            PlayerUI.SetActive(true);
            state = BattleState.PlayerTurn;
        }
        else if (state == BattleState.EnemyTurn)
        {
            if (Enemies <= 0 )
            {
                EnemyFinishedTurn();
            }
            else
            {
                if (!enemyActed)
                {
                    if (Player != null) 
                    {
                        Player.gameObject.SetActive(true);
                        PlayerMove.SetHeart();

                        EnemyAtcks0 = GameObject.FindGameObjectsWithTag(EnemyDirections[0]);
                        EnemyAtcks1 = GameObject.FindGameObjectsWithTag(EnemyDirections[1]);
                        EnemyAtcks2 = GameObject.FindGameObjectsWithTag(EnemyDirections[2]);
                        EnemyAtcks3 = GameObject.FindGameObjectsWithTag(EnemyDirections[3]);

                        EnemyAtcks = EnemyDirections[0].Concat(EnemyDirections[1], EnemyDirections[2], EnemyDirections[3]).ToArray();



                        enemyActed = true; 
                    }
                    
                }
                else
                {
                    bool enemyfin = true;
                    if (enemyfin)
                    {
                        EnemyFinishedTurn();
                    }
 
                }
            }
        }
        else if (state == BattleState.FinishedTurn)
        {
                Player.SetActive(false);

                if (PlayerHealth.HP < 0)
                {
                    state = BattleState.Lost;
                }
                else
                {
                    state = BattleState.Start;
                }


        }
        else if (state == BattleState.Won)
        {
            Debug.Log("Hai vinto!");
        }
    }
    void EnemySetDirections() 
    {
        
        EnemyDirections.Add("Directions/Up");
        EnemyDirections.Add("Directions/Down");
        EnemyDirections.Add("Directions/Left");
        EnemyDirections.Add("Directions/Right");

    }
}
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
GabrieleC0
  • 33
  • 6

1 Answers1

3

To address error itself - EnemyDirections[index] is a string, string.Concat is a static method (not an extension one) which can only be accessed via class name. Try:

string result = string.Concat(EnemyDirections[0], EnemyDirections[1], EnemyDirections[2], EnemyDirections[3]);

Note that this can be rewritten with LINQ:

string result = string.Concat(EnemyDirections.Take(4));

Or just:

string result = string.Concat(EnemyDirections);

if EnemyDirections has only 4 elements.

Though this will not fix the potential problem that you are trying to assign value to EnemyAtcks which is of type GameObject[] and not string.

Possibly you have meant something like:

EnemyAtcks = EnemyAtcks0
   .Concat(EnemyAtcks1)
   .Concat(EnemyAtcks2)
   .Concat(EnemyAtcks3)
   .ToArray();

Which uses LINQ's Enumerable.Concat<TSource> method:

Concatenates two sequences.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • 1
    Note that OP tries "all the attacks in a single list of GameObjects" - no idea why they think the code shown somehow relates to the textual description... but maybe you want to address that too. – Alexei Levenkov May 05 '23 at 21:19
  • I think i did not get it right. Is the "result" intending the "EnemyAttacks"? – GabrieleC0 May 05 '23 at 21:21
  • @GabrieleC0 added a note. `string.Concat` does what it sounds - concatenates strings into a new string. – Guru Stron May 05 '23 at 21:22
  • @GabrieleC0 check out the updates. – Guru Stron May 05 '23 at 21:27
  • It is giving me an error at line 154. NullReferenceException: Object reference not set to an instance of an object – GabrieleC0 May 05 '23 at 21:29
  • @GabrieleC0 - [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Guru Stron May 05 '23 at 21:30