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

[C#] Getting a reference to a scene object

Discussion in 'Scripting' started by kavs, Jul 17, 2012.

  1. kavs

    kavs

    Joined:
    Jun 29, 2012
    Posts:
    25
    Hey guys,

    I've looked around for a while and haven't found an answer to this, so I'm hoping someone can help me out. I'm just getting into Unity and am hitting a wall getting references to objects that exist in my scene. For example, I'm adding a proxy screenflow right now and want to trigger a texture swap on a GUITexture, but I have no idea how to get the reference to that GUITexture in my script.

    Following the Unity Scripting guide, they mentioned that I could use GameObject.Find() and pass in the object or tag name.

    Code (csharp):
    1.  
    2. Camera cam1 = GameObject.Find("mainCamera");
    3.  
    The problem with this is that it returns a GameObject, not a Camera, so I get compiler errors. What step am I missing to get this to compile and work properly? Can anyone link me to an example/source of something simple, like perhaps changing a GUITexture in code?

    TL;DR: How do I reference scene objects via script, so I can control their properties in code?

    Thanks in advance!
     
  2. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    937
    You're only getting the actual object now. Note that the camera itself is a component of the object. So you should use GetComponent if you want to have the camera

    Code (csharp):
    1.  
    2. Camera cam1 = GameObject.Find("mainCamera").GetComponent<Camera>();
    3.  
     
  3. kavs

    kavs

    Joined:
    Jun 29, 2012
    Posts:
    25
    Oh, I see! I didn't think of it broken down that far. Is it possible for a GameObject to have more than one of the same Component? Say it had 2 Cameras, or 2 lights. How would you break it down further?

    Also, one more quick question... Is there a way to create an 'empty' GameObject in the scene? I couldn't find the option in the dropdown menu, but it would be really handy to create a dummy object that I can attach my scripts to instead of using a scene item.

    Thanks so much, wasstraat. :D
     
  4. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    937
    Yes there is, its the most top one option ( above create other). This way you make an empty gameObject. As of the more than 1 component, it is possible. Just use the GetComponents() (note the S behind here). This will return an array of the specific component type. ;)
     
  5. kavs

    kavs

    Joined:
    Jun 29, 2012
    Posts:
    25
    Thanks wass! :D You saved my sanity, here.
     
  6. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    937
    No problem

    If you have some spare time left you can also take a look at my WIP :D
    Links in my signature ;)
     
  7. kavs

    kavs

    Joined:
    Jun 29, 2012
    Posts:
    25
    Ay I took a look; pretty impressive for just learning programming! I'll keep an eye on it as you move forward!
     
  8. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    937
    Thanks! ;)