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

Shorten lines?

Discussion in 'Scripting' started by SnyFort6498, Mar 14, 2017.

  1. SnyFort6498

    SnyFort6498

    Joined:
    Jan 6, 2017
    Posts:
    23
    Sorry, im new in C# programming. For example:

    GameObject.Find ("SnakePart")

    I want to make a variable of this line called "snake" so, everytime i have to call it, i write "snake" instead of writting the entire line. I tried with something like:

    GameObject snake = GameObject.Find ("SnakePart");

    However, it doesnt work
     
  2. GameNative

    GameNative

    Joined:
    Apr 17, 2014
    Posts:
    40
    Make sure the GameObject you are looking for is Active in the scene hierarchy.
     
    TaleOf4Gamers likes this.
  3. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    You are doing this inside of a function. If you want to access it elsewhere, make it global by placing outside of a function and call GameObject.Find within the Start function.
     
  4. SnyFort6498

    SnyFort6498

    Joined:
    Jan 6, 2017
    Posts:
    23
    Sorry, found the problem. I should have tried "transform" because i tried to select a child.

    Thanks everyone