Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Changing UI Text To A Variable

Discussion in 'Scripting' started by ATLAS-INTERACTIVE, Feb 16, 2015.

  1. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I am working on a dynamic model viewer for the Unity GUI, The code below is the main script that can be linked up to UI buttons via the Load Next and Load Previous functions.
    I am trying to work out how to print the name of the object, or preferably a name I can set myself in the list into a UI Text component on my UI.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class ModelViewer : MonoBehaviour
    6. {
    7.     [System.Serializable]
    8.     public class ModelViewerInformation
    9.     {
    10.         public GameObject Model;
    11.         public List<AnimationClip> Animations;
    12.     }
    13.  
    14.     public ModelViewerInformation[] Models;
    15.  
    16.     public int ModelIndex;
    17.     void UpdateModelViewer()
    18.     {
    19.      
    20.         for (int i = 0; i < transform.childCount; i++)
    21.         {
    22.             Destroy(transform.GetChild(i).gameObject);
    23.         }
    24.  
    25.  
    26.         GameObject SpawnedModel = Instantiate(Models[ModelIndex].Model, transform.position, transform.rotation) as GameObject;
    27.         SpawnedModel.transform.parent = transform;
    28.         Debug.Log("ModelUpdated");
    29.     }
    30.  
    31.    public void LoadPrevious() {
    32.  
    33.         if (ModelIndex > 0)
    34.         {
    35.             ModelIndex -= 1;
    36.         }
    37.         else
    38.         {
    39.             ModelIndex = Models.Length - 1;
    40.         }
    41.         UpdateModelViewer();
    42.    
    43.     }
    44.  
    45.  
    46.   public  void LoadNext() {
    47.  
    48.  
    49.         if (ModelIndex < Models.Length - 1)
    50.         {
    51.             ModelIndex += 1;
    52.         }
    53.         else
    54.         {
    55.             ModelIndex = 0;
    56.         }
    57.         UpdateModelViewer();
    58.     }
    59.  
    60.     void Update()
    61.     {
    62.         if (Input.GetKeyDown(KeyCode.LeftArrow))
    63.         {
    64.  
    65.             LoadPrevious();
    66.         }
    67.         if (Input.GetKeyDown(KeyCode.RightArrow))
    68.         {
    69.             LoadNext();
    70.        
    71.         }
    72.  
    73.  
    74.     }
    75.  
    76.     void Start() {
    77.         // well default to zero or the inspector value
    78.         UpdateModelViewer();
    79.    
    80.     }
    81. }
    82.  
     
  2. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    199
    Well if you have the name stored as a string you should be able to take it and assign it to the text object in your scene.

    First thing you need to is implement UnityEngine.UI. You can do this by adding it up at the top with the rest of them like so:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.UI; //add this
    Once you have done this, it's pretty straight forward. You can assign the UI text object you want via inspector by simply adding a variable declaration like so:

    Code (CSharp):
    1. public Text myText;
    and then whenever you want to modify the value of your UI text you simply call:

    Code (CSharp):
    1. myText.text = "my name";

    I hope this was helpful.
     
  3. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Having a little trouble implementing this into my script, this is our first time using serialized information.
    Also, I may be doing this wrong but the = in the third snippet of code gets an error.
     
  4. Sajid

    Sajid

    Joined:
    Mar 12, 2011
    Posts:
    199
    What is the error?
     
  5. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration

    Currently, my code is laid out like this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine.UI;
    5.  
    6. public class ModelViewer : MonoBehaviour
    7. {
    8.  
    9.     public Text ModelName;
    10.  
    11.  
    12.     [System.Serializable]
    13.     public class ModelViewerInformation
    14.     {
    15.         public GameObject Model;
    16.         ModelName.Text = "";
    17.         public List<AnimationClip> Animations;
    18.     }
    19.  
    20.     public ModelViewerInformation[] Models;
    21.  
    22.     public int ModelIndex;
    23.     void UpdateModelViewer()
    24.     {
    25.      
    26.         for (int i = 0; i < transform.childCount; i++)
    27.         {
    28.             Destroy(transform.GetChild(i).gameObject);
    29.         }
    30.  
    31.  
    32.         GameObject SpawnedModel = Instantiate(Models[ModelIndex].Model, transform.position, transform.rotation) as GameObject;
    33.         SpawnedModel.transform.parent = transform;
    34.         Debug.Log("ModelUpdated");
    35.     }
    36.  
    37.    public void LoadPrevious() {
    38.  
    39.         if (ModelIndex > 0)
    40.         {
    41.             ModelIndex -= 1;
    42.         }
    43.         else
    44.         {
    45.             ModelIndex = Models.Length - 1;
    46.         }
    47.         UpdateModelViewer();
    48.    
    49.     }
    50.  
    51.  
    52.   public  void LoadNext() {
    53.  
    54.  
    55.         if (ModelIndex < Models.Length - 1)
    56.         {
    57.             ModelIndex += 1;
    58.         }
    59.         else
    60.         {
    61.             ModelIndex = 0;
    62.         }
    63.         UpdateModelViewer();
    64.     }
    65.  
    66.     void Update()
    67.     {
    68.         if (Input.GetKeyDown(KeyCode.LeftArrow))
    69.         {
    70.  
    71.             LoadPrevious();
    72.         }
    73.         if (Input.GetKeyDown(KeyCode.RightArrow))
    74.         {
    75.             LoadNext();
    76.        
    77.         }
    78.  
    79.  
    80.     }
    81.  
    82.     void Start() {
    83.         // well default to zero or the inspector value
    84.         UpdateModelViewer();
    85.    
    86.     }
    87. }
    88.  
    I think I have done this wrong somehow, I am looking to be able to add a unique name to each model added to this, and for the text to change depending on which one is active, as I said, I know how to change text roughly, but in Javascript, not only do I not use C# much, but this is also our first time using serialized or listed information.