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.

  • What do you think about cryptocurrency trading?

  • My favorite bitcoin options have entered the chat. How can you figure out the cryptocurrency market on your own? Perhaps. To do this, look at the information on this site and you will be very surprised by what you read. As an experienced trader, I sometimes confuse what it is. I hope that you will be able to sort out everything you find on this site.

  • Oh, if there were codes for money in real life, it would be just too good. But at least it's possible to make money on the internet, and I have to say that it's pretty close. For instance, I've heard that forex trading was a great source of income, and I managed to find this page https://www.pulse.ng/business/what-services-are-must-provided-by-the-best-forex-broker/753tyj9 that can help me on my way to becoming a trader, and as I know, it can be pretty profitable.

Sign In or Register to comment.