Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
4 captures
13 Jun 22 - 14 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 trlayne0621 · May 24 at 05:45 AM · scripting problembooleansif-statement

How to remove a game object and replace it with a different one when all objects are gone

I'm really new to coding and only know so much, and my strategies that I've been using up to this point don't work for this issue. There are 4 objects the player is supposed to pick up, and Each object that you pick up is supposed to set a bool to false. It does do that, however my if statement doesn't seem to be even recognising that the bools are set to false and it's supposed to do something. It's probably something simple. Any explanation would be appreciated!

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class AllCanisters : MonoBehaviour { public GameObject CanisterGray; public GameObject FindCanister; public GameObject SwitchButton; CanisterGrab CanisterAll;

 // Start is called before the first frame update
 void Start()
 {
     CanisterAll = CanisterGray.GetComponent<CanisterGrab>(); //Script that the bools are originally set to "true" in.
 }

 // Update is called once per frame
 void Update()
 {
     if(CanisterAll.BlueC == false && CanisterAll.RedC == false && CanisterAll.GreenC == false && CanisterAll.PurpleC == false) //Doesn't seem to be doing anything but it's supposed to see if the bools are set to false since whenever you pick up an object, the bool for it gets set to false.
     {
         FindCanister.SetActive(false); //Remove
         SwitchButton.SetActive(true); //Replace
     }
 }

}

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by rh_galaxy · May 24 at 12:08 PM

The important code is where you set CanisterAll.BlueC (and the others both to true and then false, is it in CanisterGrab?). It is also important to know how you assign CanisterGray in the Inspector.

Comment
Add comment · Show 4 · 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 trlayne0621 · May 24 at 08:36 PM 0
Share

i have it set to where all the bools are set to true in CanisterGrab. CanisterGray is the game object with the script, and I have everything assigned in unity.

avatar image trlayne0621 · May 24 at 08:38 PM 0
Share

also i forgot to show it but the bools are set to true in the script

avatar image trlayne0621 · May 24 at 11:14 PM 0
Share

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class CanisterGrab : MonoBehaviour { public int layer_mask; public GameObject pedestalCanister; public GameObject hiddenCanister; public GameObject Gate;

 //Objective stuff
 public GameObject NoObjective;
 public GameObject FindCanister;
 public GameObject Blue;
 public GameObject Green;
 public GameObject Red;
 public GameObject Purple;
 public GameObject SoThatsHowItWorks;
 public GameObject SwitchButton;

 public bool BlueC;
 public bool GreenC;
 public bool RedC;
 public bool PurpleC;

 public Camera opCam;
 public float range = 1.5f;
 // Start is called before the first frame update
 void Start()
 {
     layer_mask = LayerMask.GetMask("11");
     pedestalCanister.SetActive(false);
     hiddenCanister.SetActive(true);

     //Objective stuff again
     NoObjective.SetActive(true);
     FindCanister.SetActive(false);
     Blue.SetActive(false);
     Green.SetActive(false);
     Red.SetActive(false);
     Purple.SetActive(false);
     SoThatsHowItWorks.SetActive(false);
     SwitchButton.SetActive(false);
 }

 // Update is called once per frame
 void Update()
 {
     RaycastHit hit;
     
     if (Physics.Raycast(opCam.transform.position, opCam.transform.forward, out hit, range, layer_mask))
     {
         if(Input.GetKeyDown(KeyCode.E))
         {
             hiddenCanister.SetActive(false);
             pedestalCanister.SetActive(true);
             
             NoObjective.SetActive(false);
             FindCanister.SetActive(true);
             Blue.SetActive(true);
             Green.SetActive(true);
             Red.SetActive(true);
             Purple.SetActive(true);

             Gate.GetComponent<Animator>().Play("GateLower");
             Gate.GetComponent<AudioSource>().Play();
             pedestalCanister.GetComponent<AudioSource>().Play();
         }
     }
 }

}

Here's the script.

avatar image rh_galaxy trlayne0621 · May 24 at 11:23 PM 0
Share

It doesn't seem that you ever set public bool BlueC to false, if it is true at the beginning?

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

288 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 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 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

If statement executes, even when its false again 2 Answers

How to run the same script multiple times in code but with different instances of it 0 Answers

Issue with if-statements requiring two conditions. 1 Answer

Enabling & Disabling Script via On Click 1 Answer

How to Trigger/collision only once time per gameobject hit's? 0 Answers


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