Value doesn't change in game when updated.
I have two scripts one called score, which calculates the score then converts it to a string. The other script is called Score Multiplier, that updates the multiplier value in the score script when I pick up the multiplier object.
Score Script:
public float score = 0.0f;
public float defaultScore;
public float multiplierScore;
public float multiplierNum = 0;
public TextMeshProUGUI prefab;
void Update()
{
defaultScore = Time.deltaTime * 15;
multiplierScore = defaultScore *= multiplierNum;
score += multiplierScore;
prefab.text = ((int)score).ToString("0000000");
}
Score Multiplier script:
public GameObject pickupEffect;
public GameObject Score;
public float duration = 10f;
public void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
PickUp();
}
}
public void PickUp()
{
Instantiate(pickupEffect, transform.position, Quaternion.identity);
Score multiplier = Score.GetComponent<Score>();
multiplier.multiplierNum = 1000;
Destroy(gameObject);
}
So when the player collides with the multiplier object the multiplierNum float value should update to 1000. Instead the value updates when I restart the game or when I die and respawn.
This is the respawn code in the player controller:
if (health <= 0)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
I just started using unity and coding about 4 days ago so please make your responses nood friendly.
Thanks.
hello can you put a debug.log inside the update method? thats an extrange behaviour since you dont have any Awake/enable/start method that could be updating the text. 100% sure that code is the one updating the text?
The score script updates the score value like it should, but it doesn't update multiplierNum value when it is changes through the other script until I respawn or restart the game.
Never $$anonymous$$d I made the multiplier object a prefab so it wouldn't update. Is there a way to update a prefab's values?
Answer by xxmariofer · Jan 31, 2019 at 11:32 PM
the only solutions i can think of is finding the object in the scene, or savint the multiplier value in playerprefs for example, and access the playerprefs whenever you need it.
Answer by ray2yar · Feb 01, 2019 at 12:35 AM
@alexpensotti - I saw in your comment earlier you asked about updating the value of a prefab... I assume you mean after having instantiated it... there's a couple ways to do this. Let's assume your variable is public.... so... here's one way...
GameObject MyCopy = Instantiate(//the prefab) as GameObject;
MyCopy.GetComponent<ScriptOnObject>().VariableToChange = newValue;
Your answer
Follow this Question
Related Questions
What to use instead of GameObject.Find and GetComponent 2 Answers
How to override constructor when creating a new element in a list from the inspector. 0 Answers
GetComponents() returns null while GetComponent() returns non-null. WHY 1 Answer
Override constructor of list of classes when creating a new element from the inspector 0 Answers
How to Setactive(true) the other class ? 0 Answers