Search Unity

Unable to access public string of my class

Discussion in 'Scripting' started by elpuerco63, Jan 13, 2018.

  1. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    I have a GameObject used as a prefab and it has this code attached:

    Code (csharp):
    1.  
    2. public class PrefabScript : MonoBehaviour {
    3.  
    4.     public string prefabText;
    5.  
    6.     void Start () {
    7.        
    8.        // do stuff here on instantiation to populate prefabText
    9.  
    10.         // but for now...
    11.         prefabText = "any old random text just to test!";
    12.     }
    13. }
    14.  
    The prefab has a tag of MyPrefab and when I want to grab the text from prefabText once it has been instantiated I do this:

    Code (csharp):
    1.  
    2. public void GrabText() {
    3.  
    4.         GameObject go = GameObject.FindGameObjectWithTag ("MyPrefab");
    5.  
    6.         PrefabScript ps = go.GetComponent<PrefabScript> ();
    7.  
    8.         if (ps != null) {
    9.            
    10.             print( ps.prefabText);
    11.         }
    12.     }
    13.  
    But I always get nothing printed?

    In the hierarchy I check the instantiated prefab and its prefabText field has text set yet when I try to access it I gat nothing but an empty string?

    Che?

    Edited after grizzly post...
     
    Last edited: Jan 13, 2018
  2. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    285
    Code (CSharp):
    1. print(ps.prefabText);
    :)
     
  3. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    sorry my bad in posting, yes I have ps.prefabText in the code, I just typed it wrong here! Sorry, so I still get empty string :-(
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,713
    Do you instantiate and then try to grab the string immediately?
    If that's the case, then the Start() method has not yet run. You would need to move the string assignment to Awake() or you would have to delay the method that gets the string.
     
    elpuerco63 likes this.
  5. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    Ah, thanks! I will have a change around re the sequence and see if you are right :)
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,713
  7. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    Why thank you! :):):):):)

    Turns out it was a timing thing as you suggested, I would never have figured that out! All I had to do was place a 1 second delay before I accessed the string and bingo.

    Been on this for two whole days!!!!

    Thanks :)
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,713
    Cool, glad I could help ya sort it out :)

    fyi, I believe you could also do a yield in a coroutine for WaitForEndOfFrame and that would be sufficient (time), also. :)
    If the 1 second is not bothering you, though, doesn't matter.
     
    elpuerco63 likes this.
  9. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
    It actually looks like I don't even need to do that! I was trying to access the text way too early. After revisiting the logic I can lose the delay, but this was an interesting issue, one I had not come across before re that timings involved re start, awake and accessing etc so will prove very useful knowledge to now have. Thank you :)
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,713
    Cool :) That's good stuff to know. Take it easy, and enjoy your game :)
     
    elpuerco63 likes this.
  11. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    285
    Glad to hear it's sorted! It's worth mentioning that Unity does provide some control over execution order. You can find this here: https://docs.unity3d.com/Manual/class-MonoManager.html
     
    elpuerco63 likes this.
  12. elpuerco63

    elpuerco63

    Joined:
    Jun 26, 2014
    Posts:
    271
unityunity