Hi guys,
very new to unity and this forum making a text based game,
not exactly game code but trying to work out a few functions.
just have a little code here:
what i basically want to happen is a value to increase over time every second by 1. and to show this,
and to have the rate determined by reputation.
thanks!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Displaytest : MonoBehaviour {
public UnityEngine.UI.Text Test;
public int patients = 0;
public int reputation = 1;
void Update ()
{
{
Test.text = "Patients: "+ reputation; //displays "Gold ___"}
}
{
StartCoroutine (incoming());
}
IEnumerator incoming()
{
yield return new WaitForSeconds(1);
patients = patients + reputation;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Displaytest : MonoBehaviour {
public UnityEngine.UI.Text Test;
public int patients = 0;
public int reputation = 1;
void Update ()
{
{
Test.text = "Patients: "+ reputation; //displays "Gold ___"}
}
{
StartCoroutine (incoming());
}
IEnumerator incoming() //this here is where i get the error
{
yield return new WaitForSeconds(1);
patients = patients + reputation;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Displaytest : MonoBehaviour {
public UnityEngine.UI.Text Test;
public int patients = 0;
public int reputation = 1;
public bool stopIncrease = false;
void Start()
{
StartCoroutine(incoming());
}
IEnumerator incoming()
{
while (stopIncrease == false) // continue until stopIncrease == true
{
yield return new WaitForSeconds(1); // wait for 1 second
reputation = reputation + 1; // increase the value
OnPatientReputationUpdated(reputation); // call the function that will take care of the new value
}
}
void OnPatientReputationUpdated(int newReputationValue)
{
Test.text = "Patients: "+ newReputationValue; // update your text when your value is updated
}
}
thank you very much for your help guys and girls, so quick!
and working wonderfully
Glad to help you,
remember to avoid using the Update function for this kind of behaviour. Simply assume that if a value change every second you dont need to update each frame !
Last thing I need, So this was the last code that fully works,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Displaytest : MonoBehaviour {
public UnityEngine.UI.Text Test;
public int patients = 0;
public int reputation = 1;
public bool stopIncrease = false;
public int newReputationValue;
void Start()
{
StartCoroutine(incoming());
}
IEnumerator incoming()
{
while (stopIncrease == false) // continue until stopIncrease == true
{
yield return new WaitForSeconds(1); // wait for 1 second
patients = patients + Random.Range(1,5); // increase the value
OnPatientReputationUpdated(patients); // call the function that will take care of the new value
}
}
void OnPatientReputationUpdated(int newReputationValue)
{
Test.text = "Patients: "+ newReputationValue; // update your text when your value is updated
}
}
I want to reference the newReputationValue, and display it as a text,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Reputation : MonoBehaviour {
public UnityEngine.UI.Text Try;
public int newReputationValue;
public int Rep;
void Start ()
{
GameObject.Find("Counter").GetComponent<Displaytest>().newReputationValue = Rep;
Try.text = "Reputation: "+ Rep; // the problem is here but i dont understand why
}
}
This is thoroughly confusing. You have newReputationValue, not GameObject.Find(“Counter”).GetComponent().newReputationValue.
Rep is never assigned a value. If in line 18 you want to assign a value to Rep, then the variable needs to be on the left hand side as:
Rep= GameObject.Find(“Counter”).GetComponent().newReputationValue;
But critical here, is that Rep has no value.