why doesnt change my money float?
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Money : MonoBehaviour {
public float money = 100;
public float Prize = 0;
// Use this for initialization
void Start () {
GetComponent<Text>().text = money.ToString();
}
// Update is called once per frame
void Update () {
GetComponent<Text>().text = money.ToString();
}
public void BuyAHouse()
{
Debug.Log("test");
money = Prize;
}
}
Answer by Lo0NuhtiK · Aug 14, 2017 at 06:27 PM
Are you calling the "BuyAHouse()" method anywhere at all? Does your debug show in the console?
You should cache your Text component also instead of using GetComponent all the time.
You should also only change the text value when the money value is changed, rather than updating it all the time.
//cache for Text component
private Text txt;
public float money = 100f;
public float prize = 0f;
void Start()
{
//caching the component in Start()
txt = GetComponent<Text>();
txt.text = money.ToString();
}
void Update()
{
//press "X" key to call BuyHouse() method
if(Input.GetKeyDown(KeyCode.X)) BuyHouse();
}
void BuyHouse()
{
Debug.Log("Buying A House...");
money = prize;
txt.text = money.ToString();
}
if i call BuyHouse From another script than doesnt work it
using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEngine;
public class BuildSystem : $$anonymous$$onoBehaviour { public $$anonymous$$oney moneyScript;
void Update()
{
if (Input.Get$$anonymous$$eyDown("b"))
{
if (moneyScript.money >= 100)
{
moneyScript.BuyHouse();
}
}
}
}
Answer by $$anonymous$$ · Aug 14, 2017 at 08:01 PM
it is working but the console is saying Object reference not set to an instance of an object
Script line: txt.text = money.ToString();
Your answer
Follow this Question
Related Questions
How to do multiple inheritance (or workaround) with Unity? 1 Answer
currentSpeed doesnt change when pressing Shift (C#) 1 Answer
Issues with OnMouseOver() not working 2 Answers
[SOLVED!] PlayerPrefabs SetFloat and GetFloat is not working 0 Answers
Why isn't my C# code sending updates to my text scoreboard? 1 Answer