How do I change the scene after 2 objects are destroyed (SetActive false)
Sorry, this may be a beginner question but I'm having trouble changing the scene after two objects are destroyed (set active false) I have a game where the goal is to shoot all of the targets. I want it so that when the 2 objects disappear, the player proceeds to the next scene/next level. The actual scene change works, just not the if statement.
Here is my current script:
void Start ()
{
}
void Update ()
{
if (gameObject.SetActive (false))
{
SceneManager.LoadScene ("Level2");
}
}
(By the way, I have the lines before the void Start thing all fine)
When I run this script an error message like this shows up next to the "if (gameObject.SetActive (false))" line:
Cannot implicitly convert type 'void' to 'bool'
This message points to the S in "SetActive" on that line.
I'm new to this scripting stuff so I don't know what these error messages mean. I know it's probably really easy but thanks in advance!
I'm trying to make it so that I can do something with an if-statement of the GameObject set active is false.
So if (gameobject setactive false) { change scene }
I have the correct script for changing the scene, but the If statement isn't working properly.
Answer by SohailBukhari · Jul 15, 2017 at 04:45 PM
check gameObject.activeInHierarchy
in the condition. GameObject.activeInHierarchy represents if the object is active within the entirety of the scene. An object can be made inactive if their gameobject or any of their parent's gameobjects have been set to inactive.
if (gameObject.activeInHierarchy)
{
SceneManager.LoadScene ("Level2");
}
Well, the script didn't have any error message when I ran it but it's not doing what I wanted it to when I test it out. Nothing really happens when I shoot and disable the target.
So now I have this script (I changed it to other)
{ public GameObject other; void Start () {
}
void Update ()
{
if (other.gameObject.activeInHierarchy)
{
Scene$$anonymous$$anager.LoadScene ("Level2");
}
}
}
But when I shoot the target and it's Set Active goes false, the scene doesn't change.
just negate the statement in the condition, will retuen true when your gameObject will be inactive.
void Update ()
{
if (!other.gameObject.activeInHierarchy) //add !
{
Scene$$anonymous$$anager.LoadScene ("Level2");
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612130340im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity 5 checking if player isGrounded 1 Answer
Changing Script Based On Scene 1 Answer
How can I check the light intensity??? 0 Answers