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

Using EditorGUILayout . ObjectField how do I access the GameObject not Object?

Discussion in 'Scripting' started by supernamey923834, Apr 14, 2021.

  1. supernamey923834

    supernamey923834

    Joined:
    Jul 27, 2017
    Posts:
    10
    The main item is EditorGUILayout.ObjectField target. I would like to make a script window that I can drab an object into and press a button to add a whole bunch of components to with custom settings.
    Right now it seems the targetObject is just type of Object and I can not access its GameObject

    I tried various like
    targetObject.activeGameObject....
    but nothing in the code analyzer or autocomplete is giving me a hint

    Code (CSharp):
    1.  
    2. targetObject = EditorGUILayout.ObjectField(targetObject, typeof(GameObject) , true) as GameObject;
    3.  
    full code

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. #if UNITY_EDITOR
    6. using UnityEditor;
    7. //using UnityEditorInternal;
    8. #endif
    9.  
    10. public class SetupToolWindow : EditorWindow
    11. {
    12.  
    13.     public Object targetObject;
    14.  
    15.     [MenuItem("NERF/Auto Setup tools")]
    16.     static void Init()
    17.     {
    18.         var window = (SetupToolWindow)GetWindow(typeof(SetupToolWindow));
    19.         window.Show();
    20.     }
    21.  
    22.     void OnGUI()
    23.     {
    24.         // sizeMultiplier = EditorGUILayout.FloatField("Increase scale by:", sizeMultiplier);
    25.  
    26.         GUILayout.Label("Doo stuffff", EditorStyles.boldLabel);
    27.  
    28.         EditorGUILayout.BeginHorizontal();
    29.         targetObject = EditorGUILayout.ObjectField(targetObject, typeof(GameObject) , true) as GameObject;
    30.         EditorGUILayout.EndHorizontal();
    31.  
    32.  
    33.  
    34.         if (GUILayout.Button("Magic"))
    35.         {
    36.             if (targetObject)
    37.             {
    38.                 Debug.Log(targetObject.name);
    39.                 Rigidbody rb = targetObject.activeGameObject.
    40.             }
    41.             // if (!Selection.activeGameObject)
    42.             // {
    43.             //     Debug.Log("Select a GameObject first");
    44.             //     return;
    45.             // }
    46.  
    47.             // Selection.activeTransform.localScale =
    48.             //     Selection.activeTransform.localScale * sizeMultiplier;
    49.         }
    50.     }
    51. }
    52.  
    53.  
    54.  
    55.  
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,196
    you probably want:

    Code (csharp):
    1. public GameObject targetObject;
     
  3. supernamey923834

    supernamey923834

    Joined:
    Jul 27, 2017
    Posts:
    10
    WELLLLL DERP. Thank you!