Search Unity

Question Extremely Basic Question - Field in Update method not Updating

Discussion in 'Getting Started' started by jf065d, Feb 13, 2022.

  1. jf065d

    jf065d

    Joined:
    May 28, 2018
    Posts:
    23
    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

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class MultiNoteHitCounter : MonoBehaviour
    7. {
    8.     public TextMeshPro multiNoteHitCounterText;
    9.     public string multiNoteHitCounterString;
    10.     public float multiNoteCounterSpeed;
    11.  
    12.     public void SetMultinoteHitCount(string hitCountString)
    13.     {
    14.         multiNoteHitCounterString = hitCountString;
    15.         multiNoteHitCounterText.SetText(multiNoteHitCounterString);
    16.         Debug.Log("Method variable is: "+multiNoteHitCounterString);
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         gameObject.transform.position -= new Vector3(multiNoteCounterSpeed * Time.deltaTime, 0f, 0f);
    22.         Debug.Log("Update variable is: " + multiNoteHitCounterString);
    23.  
    24.     }
    25.  
    26.     public void SetMultinoteHitCounterSpeed(float setMultinoteHitCounterSpeed)
    27.     {      
    28.         multiNoteCounterSpeed = setMultinoteHitCounterSpeed;
    29.     }
    30. }
    31.  
     
  2. jf065d

    jf065d

    Joined:
    May 28, 2018
    Posts:
    23
    In this variation, I'm using a set accessor. The debug log in the accessor shows "multiNoteCounterTrigger" as true, and yet the if statement in the Update method does NOT run.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using TMPro;
    5.  
    6. public class MultiNoteHitCounter : MonoBehaviour
    7. {
    8.     public TextMeshPro multiNoteHitCounterText;
    9.     private string multiNoteHitCounterString;
    10.     public float multiNoteCounterSpeed;
    11.     private bool multiNoteCounterTrigger;
    12.  
    13.     void Update()
    14.     {
    15.         gameObject.transform.position -= new Vector3(multiNoteCounterSpeed * Time.deltaTime, 0f, 0f);
    16.        
    17.         if(multiNoteCounterTrigger == true)
    18.         {
    19.             Debug.Log("Update variable is: " + multiNoteHitCounterString + "and Trigger is:" + multiNoteCounterTrigger);
    20.             multiNoteCounterTrigger = false;
    21.         }      
    22.     }
    23.  
    24.     public string MultiNoteHitCounterString
    25.     {
    26.         set
    27.         {
    28.             multiNoteHitCounterString = value;
    29.             multiNoteCounterTrigger = true;
    30.             Debug.Log("Setter works and: " + multiNoteHitCounterString + "and Trigger is:" + multiNoteCounterTrigger);
    31.         }
    32.     }
    33.  
    34.     public void SetMultinoteHitCounterSpeed(float setMultinoteHitCounterSpeed)
    35.     {      
    36.         multiNoteCounterSpeed = setMultinoteHitCounterSpeed;
    37.     }
    38. }
    39.  
     
  3. jf065d

    jf065d

    Joined:
    May 28, 2018
    Posts:
    23
    I think I figured it out:
    Because I am Instantiating a new instance of my Prefab 2, I need to specifically call the update methods for that given instance.
    So, in the script of Prefab 1 (where I'm Instantiating Prefab 2), instead of this:
    • multinoteHitCounterScript.SetMultinoteHitCount(multiNoteHitCount.ToString());
    I'm using this:
    • multinoteHitCounterInstance.GetComponent<MultiNoteHitCounter>().SetMultiNoteCounter(multiNoteHitCount.ToString());

    Not sure if this is the correct way of doing it, but it seems to be working. Hope that helps if others run into the same issue of their Update method not seemingly receiving any updates to its methods. :)