Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Help with money

I'm making a 2d tycoon game and I've written a quick money manager script, but I don't understand something. How do I get the objects with a certain tag and then convert them into an integer?


using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;


public class MoneyManager : MonoBehaviour

{

public Text money;

public float moneyAmount = 10000f;

public float moneyChange = 100f;

public int workers;



// Start is called before the first frame update

void Start()

{

}


// Update is called once per frame

void Update()

{

workers = workers.FindGameObjectsWithTag("Worker");


workers = System.Convert.ToInt16(workers);


MoneyChange();

}


void MoneyChange()

{

money.text = moneyAmount.ToString();

}


IEnumerator MoneyAdd ()

{

yield return new WaitForSeconds(60);

moneyChange = moneyChange * workers;

moneyAmount = moneyAmount + moneyChange;

}

}

Answers

  • workers.Length

  • which part?

  • I just saw that workers, is already an int, not an array. Same thing though.

    workers = FindGameObjectsWithTag("Worker").Length;

  • I did that, now it gives me this error: Assets/MoneyManager.cs(23,27): error CS1061: 'int' does not contain a definition for 'FindGameObjectsWithTag' and no accessible extension method 'FindGameObjectsWithTag' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?)

  • JIMMY_VASHI04JIMMY_VASHI04 Member
    edited July 2020

    Use

    If your desired int is this worker tagged objects child use

    Workers = FindGameObjectWithTag("your tag").ChildCount

    If its in some script you have to reference the script first with GetComponent then access whatever you want

  • MouledouxMouledoux Member
    edited July 2020

    No, you just need to remove the "worker." From the find objects

    FindObjectsWithTag instead of workers.Find

  • Assets/MoneyManager.cs(23,19): error CS0103: The name 'FindGameObjectsWithTag' does not exist in the current context


    @Mouledoux

  • Can you post the code too please?

  • Sure:


    using System.Collections;

    using System.Collections.Generic;

    using UnityEngine;

    using UnityEngine.UI;


    public class MoneyManager : MonoBehaviour

    {

    public Text money;

    public float moneyAmount = 10000f;

    public float moneyChange = 100f;

    public int workers;



    // Start is called before the first frame update

    void Start()

    {

    }


    // Update is called once per frame

    void Update()

    {

    workers = FindGameObjectsWithTag("Worker").Length;


    workers = System.Convert.ToInt16(workers);


    MoneyChange();

    }


    void MoneyChange()

    {

    money.text = moneyAmount.ToString();

    }


    IEnumerator MoneyAdd ()

    {

    yield return new WaitForSeconds(60);

    moneyChange = moneyChange * workers;

    moneyAmount = moneyAmount + moneyChange;

    }

    }

  • remove the convert to int16. I though you could call Find commands without an instance, but try adding "gameObject." to your Find.

  • Assets/MoneyManager.cs(23,19): error CS0176: Member 'GameObject.FindGameObjectsWithTag(string)' cannot be accessed with an instance reference; qualify it with a type name instead

  • lowercase gameObject. we're referencing the object this script is on.

  • It is lower case

  • My mistake, Unity docs says it should be Uppercase.

  • Ah I'll try that tmrw it's too late to do it now but thank you :)

  • DenizDeniz Member

    Try :

    workers = Find.GameObjectsWithTag("Worker").Length;
    

    or :

    workers = GameObject.FindWithTag("Worker").Length;
    

    or :

    workers = GameObject.FindWithTag("Worker");
    

    I think the last one can work..

  • Are there any codes for money in real life? I could really use something like that. I even consider trading at this point. My friend recommended me https://www.ironfx.com/en/ironfx-trading-school, and I'm seriously thinking about getting involved in this sphere. Hopefully, I won't lose all my money on the first day, and it won't be too hard for me.

Sign In or Register to comment.