It looks like you're new here. If you want to get involved, click one of these buttons!
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?)
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
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 :)
Try :
or :
or :
I think the last one can work..
See this link you will get every answer