Search Unity

How do i print selected object name on this GUI

Discussion in 'Scripting' started by Goldensnitch, Dec 20, 2016.

  1. Goldensnitch

    Goldensnitch

    Joined:
    Nov 4, 2016
    Posts:
    60
    I have this GUI prints out numbers of objects i select and rather than printing this i want to print out object name on it. How do i do this please help me

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. namespace SimpleSelect
    6. {
    7.     public class TestGUI : MonoBehaviour
    8.     {
    9.         private Rect labelRect = new Rect(5, 5, 250, 25);
    10.  
    11.         void Update()
    12.         {
    13.             if(Input.GetKeyDown("d"))
    14.             {
    15.                 GameObject[] obj = Selections.GetSelectedObjects();
    16.                 for(int i = 0; i < obj.Length; i++) GameObject.Destroy(obj[i]);
    17.             }
    18.         }
    19.  
    20.         void OnGUI()
    21.         {
    22.             GUI.Box(this.labelRect, "You selected " + Selections.GetSelectionCount() + " of " + Selections.GetSelectableCount() + " objects!");
    23.         }
    24.     }
    25. }
    26.  
    27.  
     
  2. Khyrid

    Khyrid

    Joined:
    Oct 8, 2010
    Posts:
    1,790
    What do you mean object name?

    Check the documentation on names I guess
    https://docs.unity3d.com/ScriptReference/Object-name.html
    Otherwise simply use a string variable to assign the names of the objects.

    Code (CSharp):
    1.      
    2.        public GameObject exampleOne;
    3.  
    4.         exampleOne.name = name1; //What ever name you're using instead of name1
    5.         s = exampleOne.name.ToString();
    6.  
    Then use 's' instead of 'Selections.GetSelectionCount()' I guess.
    That is if I'm even understanding what you're asking.

    Maybe you mean
    Code (CSharp):
    1. s = obj.name.ToString();
    2.  
    3. GUI.Box(this.labelRect, "You selected " + Selections.GetSelectionCount() + " of " + Selections.GetSelectableCount() + s + "(s)");