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

Resolved SetActive doesn't Activate my video player and raw image components

Discussion in 'Scripting' started by Hexolenn, May 10, 2023.

  1. Hexolenn

    Hexolenn

    Joined:
    Dec 27, 2021
    Posts:
    44
    I have 2 components under my UI. These are Video Player and Raw Image to make a background video. They are initially inactive so that it doesn't play in a wrong menu. But after I try to activate them using SetActive they don't become active.

    upload_2023-5-10_16-17-17.png Here is the code that activates my video player and raw image

    upload_2023-5-10_16-18-26.png Here is the video player and raw image components

    The problem isin't me trying to activate them in an inactive script I have already tested that and it can enter this script

    upload_2023-5-10_16-20-8.png I can even get in the component and get the name of the clip I want to play as background.

    But it doesnt activate my components. What is wrong with my code ?
     
  2. Hexolenn

    Hexolenn

    Joined:
    Dec 27, 2021
    Posts:
    44
    I have realised the problem. The problem was that for some reason my code doesn't try to activate my components but the game object itself which is the UI.

    upload_2023-5-10_17-3-43.png
    I have written a code that would close the components to test it out but instead of maybe closing the components this close method deactivates the UI itself which the Video Player and the Raw Image is on.

    Knowing this how can I access these components to activate or deactivate them ?
     
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,429
    Uhm you do know that only gameobjects have an active state (more specifically two active states activeSelf and activeInHierarchy). Components do not have an active state, they can have an enabled state, but not all components have one. Specifically all Behaviour derived components have an enabled property and a few other built-in components. So since your components are disabled, you just want to enable them by setting the enabled property of that component to true.

    If a gameobject is deactivated, components on that gameobject would also not work anymore regardless of their enabled state. A gameobject is only truly active when all it's parents are active as well. That's why there's the difference between activeSelf and activeInHierarchy.
     
  4. Hexolenn

    Hexolenn

    Joined:
    Dec 27, 2021
    Posts:
    44
    Yeah I forgot about that. I just remembered that I could enable and disable them but not how to do that and got them mixed up thank you.

    I just needed to change
    transform.GetComponent<VideoPlayer>().gameObject.SetActive(true);
    to
    transform.GetComponent<VideoPlayer>().enabled = true;
     
    Bunny83 likes this.