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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Referencing GameObjects in an Editor Script

Discussion in 'Scripting' started by SprinkledSpooks, Nov 25, 2016.

  1. SprinkledSpooks

    SprinkledSpooks

    Joined:
    Jun 1, 2016
    Posts:
    117
    I'm currently writing a script that creates a menu with one sub-item and that, when that sub item is clicked, will cause a few items in the hierarchy to become deactivated. The only issue I'm having is that I don't know if it is possible to reference GameObjects from an editor script, because I would have to attach the script to something to be able to use the drag-on-drop method. First of all, is it even possible to reference GameObjects from the hierarchy in an editor script? Secondly, if it is possible, how would I go about doing so?

    Here's what I have so far:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5.  
    6. public class UIChangePage {
    7.  
    8.     [MenuItem("Tools/UI Change/Go to audio")]
    9.     private static void ChangeToAudio()
    10.     {
    11.         //Haven't written it's functionality yet
    12.     }
    13.  
    14. }
     
  2. magnite

    magnite

    Joined:
    Dec 12, 2012
    Posts:
    125
    You could create a Game Object variable and use the Editor OnGUI to expose it and allow the drag and drop functionality within the editor window. Or if you know the object and it wont change then you can just do a Resources.Load