0

I've been working on a C# script in unity governing 'units' and in short, the compiler is telling me the '(' is unexpected in my integer function - 20th line in the pasted code below - before 'int cost' and I just can't find the issue with my script anywhere. If I remove the whole function there are no errors and it compiles successfully so I'm 99% sure the issue lies in the function itself. Apologies for the indentation, new to StackOverflow and bad at adding code onto here!

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

namespace Iska.CNC.Units
{

public class UnitHandler : MonoBehaviour 
{
    public static UnitHandler instance;

    [SerializeField]
    private BasicUnit Pitbull, RaiderBuggy;

    private void Start()
    {
        instance = this;
    }

    public (int cost, int attack, int attackRange, int health, int tier) GetBasicUnitStats(string type)
    {
        BasicUnit unit;
        switch (type)
        {
            case "Pitbull":
                unit = Pitbull;
                break;
            case "RaiderBuggy":
                unit = RaiderBuggy;
                break;
            default:
                Debug.Log("Error - unit cannot be found or does not exist");
                return (0, 0, 0 ,0 ,0);
        }
        return (unit.cost, unit.attack, unit.attackRange, unit.health, unit.tier);
    }

    public void SetBasicUnitStats(Transform type)
    {
        foreach (Transform child in type) 
        {
            foreach (Transform unit in child) 
            {
                string unitName = child.name.Substring (0, child.name.Length - 1).ToLower ();
                var stats = GetBasicUnitStats (unitName);
                Player.PlayerUnit pU;

                if (type == Iska.CNC.Player.PlayerManager.instance.playerUnits) 
                {
                    pU = unit.GetComponent<Player.PlayerUnit> ();
                    //set unit stats in each unit
                    pU.cost = stats.cost;
                    pU.attack = stats.attack;
                    pU.attackRange = stats.attackRange;
                    pU.health = stats.health;
                    pU.tier = stats.tier;
                } 
                else if (type == Iska.CNC.Player.PlayerManager.instance.enemyUnits) 
                {
                    //set enemy stats
                }
                //if we have any upgrades or buffs, add them now
                //add upgrades to unit stats
            }
        }
    }

}
}
2
  • 3
    This tuple syntax require you to enable C# 7 in unity. Commented Apr 23, 2021 at 7:40
  • Thanks @Sinatr that solved the issue! Commented Apr 23, 2021 at 9:00

1 Answer 1

1

Alternative you can do it with

public Tuple<int, int, int, int> GetBasicUnitStats (string type)
{
    BasicUnit unit;
    switch (type)
    {
        case "Pitbull":
            unit = Pitbull;
            break;
        case "RaiderBuggy":
            unit = RaiderBuggy;
            break;
        default:
            Debug.Log("Error - unit cannot be found or does not exist");
            return Tuple.Create(0, 0, 0 ,0 ,0);
    }

    return Tuple.Create(unit.cost, unit.attack, unit.attackRange, unit.health, unit.tier);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.