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

How to enable a disables GameObject

Discussion in 'Scripting' started by DanWef, Nov 4, 2020.

  1. DanWef

    DanWef

    Joined:
    Jun 4, 2020
    Posts:
    27
    Hi! I wanna activate an disabled GameObject at the end of and animation state, but I alway get the error "NullReferenceException: Object reference not set to an instance of an object". What I do wrong?

    Code (CSharp):
    1.  public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    2.     {
    3.         GameObject.Find("GridButton").gameObject.SetActive(true); ;
    4.  
    5.  
    6.     }
    Thank You!
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    GameObject.Find only searches for active game objects. You will need a different method of identifying the object you want to activate.

    The most common way to do this is to create a public variable and then assign it to the appropriate object via the inspector, but there's several ways of acquiring object references, and which one is best depends on the circumstances.
     
    Joe-Censored and PraetorBlue like this.
  3. DanWef

    DanWef

    Joined:
    Jun 4, 2020
    Posts:
    27
    Which ways are possible? Im totally new to the engine. The way with the public variable doesnt work, Im just able to add prefabs to an public gameobject variable but no excisting gameobjects of the scene. Thx!
     
  4. tobiasvanelk

    tobiasvanelk

    Joined:
    Apr 7, 2020
    Posts:
    5
    Is the animator that OnStateEnter gets as a parameter, attached to the game object that you're trying to enable? Because in that case, I think you should be able to do:

    Code (Csharp):
    1.  
    2. public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    3. {
    4.     animator.gameObject.SetActive(true);
    5. }
    6.  
     
    PraetorBlue likes this.
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    I somehow doubt this is the case because if the gameObject the Animator is attached to is disabled, the animator wouldn't be running.

    hey OP, Are you aware that the active state of the GameObject might actually be something you can animate without any extra code? Depending on where the object lives in your hiearchy, you can just add it as a property in your Animator!

    Here's a screenshot from one of my projects showing IsActive as an animatable property: upload_2020-11-4_12-36-31.png
     
    tobiasvanelk likes this.
  6. DanWef

    DanWef

    Joined:
    Jun 4, 2020
    Posts:
    27
    Thanks for the tips :) But unfortunately I havent found a solution with it. I cant see "Base" in my Animation Window. Why is that? Thanks
     

    Attached Files:

  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    In my case I literally have an object called "Base" in my prefab's hierarchy that this screenshot came from.

    upload_2020-11-4_14-7-45.png

    In general I don't think you can set active on the ROOT object of the animator's hierarchy, if that's the case in your case. if possible see if you can move the component or object you're looking to reactivate to a child object of the one that has the animator on it.
     
  8. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Prefabs can generally only have references to other parts of the prefab, but objects in a scene should be able to refer to other objects in the same scene. Is the object that needs the reference a prefab? Instead of using the prefab directly, could you create an instance of that prefab in the appropriate scene, and use that? (Possibly keeping it inactive until you need it.)

    Alternately, can you store a reference to the object you need in a variable on some other object on the scene that would be easier to find? Once you find one component, you can follow its public variables to other components that it "knows about".



    FindObjectOfType (and its plural version) have an optional parameter that allows them to search for inactive objects.

    I believe you can get to inactive objects through parent/child relations using Transform.Find and other hierarchical functions on the Transform class, but note that these only allow you to get a direct parent/child, so if you want to traverse multiple levels of hierarchy it requires multiple steps, and you need to know the right place to look at each step.

    You can store references to certain objects in public static variables somewhere, which will be accessible to whatever script needs them. (Static variables can be accessed without needing a reference to any particular class instance.) This is especially used for classes following the singleton design pattern. But you need to be careful you don't end up with multiple instances of a class all trying to use the same static variable as if it were just for them, or they'll overwrite each other.
     
  9. DanWef

    DanWef

    Joined:
    Jun 4, 2020
    Posts:
    27
    I now stored the objects I wanna activate in a new script of a new gameobject called "Referencer". I assigned all objects to the References via Inspector. Now the problem is, Im not able to access the scirpt (and the variables) of the Referencer from my Animation Script. Maybe it is a problem Im in a "StateMachineBehaviour"?
     
  10. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Post the code for the variables you are trying to reference and how you are trying to reference them.
     
    DanWef likes this.
  11. DanWef

    DanWef

    Joined:
    Jun 4, 2020
    Posts:
    27
    It WORKS! Thanks for your help :) I had just a stupid naming error. I get it done with an extra referencer object that has the diasbled objects as variables :)