Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Mrkrisher · Jul 15, 2017 at 04:33 PM · scripting problemif-statementssetactivescriptingproblemscene-change

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mrkrisher · Jul 15, 2017 at 05:08 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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");
          }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mrkrisher · Jul 15, 2017 at 05:03 PM 0
Share

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.

avatar image SohailBukhari Mrkrisher · Jul 15, 2017 at 05:55 PM 0
Share

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");
      } 
  }
avatar image Mrkrisher SohailBukhari · Jul 15, 2017 at 07:44 PM 0
Share

Got it to work thanks

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

110 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How to reassign a new script reference to the same variable that previously had the same script with another reference? 0 Answers

Set public gameobject by raycast hit target 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges