Hi,
I’ve been learning Unity for the past 2 weeks and am suddenly extremely stuck on what feels like a very basic question.
- I have a C# script attached to a Prefab TextMeshPro object (“Prefab 2”) that get’s Instantiated from another Prefab ("Prefab 1). They are not parent/child.
- On Start, Prefab 1 sends a string to Prefab 2 a method in Prefab 2 (SetMultinoteHitCount() using it’s via parameter (hitCountString)
- Prefab 2 then updates a string (multiNoteHitCounterString) and displays the value via the TextMeshPro text.
- My public “TestMeshPro” field references the SAME Prefab gameObject that this script is attached to, but that should be ok, I think?
Problem:
- In the code below, the first debug will correctly return “1”, while the second debug returns “2”, even though they’re the same public string field (multiNoteHitCounterString).
- When I change this string field to static, it fixes the problem, but then, of course, that field in different instances of my prefab start impacting each other.
- Again, this may be a very obvious issue to resolve considering I’m super new to all this.
I may also be having issues updating the textmesh after start, but I’ll cross that bridge when i get to it. Any thoughts would be greatly appreciated. I have no idea how I haven’t had this issue with other aspects of my game yet.
Thanks
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class MultiNoteHitCounter : MonoBehaviour
{
public TextMeshPro multiNoteHitCounterText;
public string multiNoteHitCounterString;
public float multiNoteCounterSpeed;
public void SetMultinoteHitCount(string hitCountString)
{
multiNoteHitCounterString = hitCountString;
multiNoteHitCounterText.SetText(multiNoteHitCounterString);
Debug.Log("Method variable is: "+multiNoteHitCounterString);
}
void Update()
{
gameObject.transform.position -= new Vector3(multiNoteCounterSpeed * Time.deltaTime, 0f, 0f);
Debug.Log("Update variable is: " + multiNoteHitCounterString);
}
public void SetMultinoteHitCounterSpeed(float setMultinoteHitCounterSpeed)
{
multiNoteCounterSpeed = setMultinoteHitCounterSpeed;
}
}