Search Unity

gameObject.SetActive

Discussion in 'Getting Started' started by Mansmart10, Jan 15, 2016.

  1. Mansmart10

    Mansmart10

    Joined:
    Dec 24, 2015
    Posts:
    43
    I am trying to activate another gameobject when my player triggers another gameobject. Can someone please help me?
     

    Attached Files:

  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I'll try. First, study the docs for GameObject.SetActive. You'll see that it takes just one parameter, which is true or false; true to make it active, false otherwise.

    There is no place in that to pass in a string, like the "NextScene" string you're attempting to pass. It applies to the GameObject you call it on, and no other.

    Next concept: what is this "gameObject" (with a lowercase "g") shown in the examples? That's a reference to the GameObject that this particular instance of your script is on. When you see "gameObject" like that, you should think "my gameObject." Me, myself, the GameObject I live on. So, the code example in the docs is deactivating itself.

    You want to activate a different game object. So your first question should be, how do I get a reference to that other game object? There are many ways to do that, but the standard first approach is to make a public property, so that when your script is attached to something in the scene, you can drag in something else from the scene as the value of that property.

    Now I don't do Unityscript, so here it is in C#.

    Code (CSharp):
    1. #using UnityEngine;
    2.  
    3. public class Enabler : MonoBehaviour {
    4.     public GameObject thingToEnable;
    5.     void function OnTriggerEnter(Collider other) {
    6.         thingToEnable.SetActive(true);
    7.     }
    8. }
    This is off the top of my head, but ought to be pretty close.

    I recommend doing some tutorials, both for Unity in general, and for C# programming (try cplusplus.com). A few hours of that will save you dozens of hours of confusion.

    Good luck,
    - Joe
     
  3. Mansmart10

    Mansmart10

    Joined:
    Dec 24, 2015
    Posts:
    43
    Thank you for your help I think I figured it out now.
     
    JoeStrout likes this.