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 /
This question was closed Sep 15, 2016 at 07:50 PM by Eagle-Bound for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Eagle-Bound · Sep 15, 2016 at 07:13 PM · c#gameobjectdisable

deactivate a object through c#

So in my really simple Simon Says type game, one of six colors will flash and the player repeats the pattern. However, the way the game works, you can press colors before and after the game is being played. How would I make it so the objects are disabled or so that they don't do anything until the start game button is pressed? Thanks

 using UnityEngine; 
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine.UI;
 
 public class GameManager : MonoBehaviour {
 
     public SpriteRenderer[] colors;
     public AudioSource[] buttonSounds;
     private int colorSelect;
     public float lit;
     private float litCounter;
     public float waitBetweenLights;
     private float waitBetweenCounter;
     public float waitBetweenLevels;
     private float waitBetweenLevelsCounter;
     private bool shouldBeLit;
     private bool shouldBeDark;
     public List<int> activeSequence;
     private int posInSequence;
     private bool gameActive;
     private int inputInSequence;
     public AudioSource correct;
     public AudioSource incorrect;
     public Text scoreText;
     public Text startGameText;
     public Button startButton;
 
     // Use this for initialization
     void Start () {
 
         if (!PlayerPrefs.HasKey ("EasyHighScore")) {
             PlayerPrefs.SetInt ("EasyHighScore", 0);
         }
 
         scoreText.text = "Score: 0 - High Score: " + PlayerPrefs.GetInt ("EasyHighScore");
         
     }
 
     // Update is called once per frame
     void Update () {
         if (shouldBeLit) {
             litCounter -= Time.deltaTime;
             if (litCounter < 0) {
                 colors [activeSequence[posInSequence]].color = new Color (colors [activeSequence[posInSequence]].color.r, colors [activeSequence[posInSequence]].color.g, colors [activeSequence[posInSequence]].color.b, 0.5f);
                 buttonSounds[activeSequence[posInSequence]].Stop();
                 shouldBeLit = false;
                 shouldBeDark = true;
                 waitBetweenCounter = waitBetweenLights;
                 posInSequence++;
             }
         }
 
         if (shouldBeDark) {
             waitBetweenCounter -= Time.deltaTime;
             if (posInSequence >= activeSequence.Count) {
                 shouldBeDark = false;
                 gameActive = true;
             } else {
                 if (waitBetweenCounter < 0) {
                     colors [activeSequence[posInSequence]].color = new Color (colors [activeSequence[posInSequence]].color.r, colors [activeSequence[posInSequence]].color.g, colors [activeSequence[posInSequence]].color.b, 1f);
                     buttonSounds[activeSequence[posInSequence]].Play();
                     litCounter = lit;
                     shouldBeLit = true;
                     shouldBeDark = false;
                 }
             }
         }
 
     }
         
 
     public void StartGame () {
         activeSequence.Clear ();
         posInSequence = 0;
         inputInSequence = 0;
         colorSelect = Random.Range (0, colors.Length);
         activeSequence.Add (colorSelect);
         colors [activeSequence[posInSequence]].color = new Color (colors [activeSequence[posInSequence]].color.r, colors [activeSequence[posInSequence]].color.g, colors [activeSequence[posInSequence]].color.b, 1f);
         buttonSounds[activeSequence[posInSequence]].Play();
         litCounter = lit;
         shouldBeLit = true;
         scoreText.text = "Score: 0 - High Score: " + PlayerPrefs.GetInt ("EasyHighScore");
         startGameText.text = "Easy";
         startButton.interactable = false;
         //Proably here the objects would be enabled
     }
 
     public void ColorPressed(int whichButton){
 
         if (activeSequence[inputInSequence] == whichButton) {
             if (gameActive) {
                     Debug.Log ("Correct");
                     inputInSequence++;
             }
                 if (inputInSequence >= activeSequence.Count) {
                     if (activeSequence.Count > PlayerPrefs.GetInt ("EasyHighScore")) {
                         PlayerPrefs.SetInt ("EasyHighScore", activeSequence.Count);
                     }
                     scoreText.text = "Score: " + activeSequence.Count + "- High Score: " + PlayerPrefs.GetInt ("EasyHighScore");
                     posInSequence = 0;
                     inputInSequence = 0;
                     colorSelect = Random.Range (0, colors.Length);
                     activeSequence.Add (colorSelect);
                     colors [activeSequence[posInSequence]].color = new Color (colors [activeSequence[posInSequence]].color.r, colors [activeSequence[posInSequence]].color.g, colors [activeSequence[posInSequence]].color.b, 1f);
                     buttonSounds[activeSequence[posInSequence]].Play();
                     litCounter = lit;
                     shouldBeLit = true;
                     gameActive = false;
                     correct.Play ();
                 }
                 } else {
                     Debug.Log ("Wrong");
                     incorrect.Play ();
                     gameActive = false;
                     startButton.interactable = true;
                 //Proably here the objects would be disabled
                 }
         }
 
     }
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

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by AurimasBlazulionis · Sep 15, 2016 at 07:36 PM

Hello.

The line you are looking for is gameObject.SetActive (true/false). Replace gameObject with an object reference which you want to enable/disable. You can store objects in an array of GameObjects and loop through it to enable or disable all of them.

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 Eagle-Bound · Sep 16, 2016 at 07:06 PM 0
Share

Thanks! do you think there would be a way to just remove a specific part of the game object? like the box collider or the audio source?

avatar image AurimasBlazulionis Eagle-Bound · Sep 17, 2016 at 05:34 AM 0
Share

so then you need to do gameObject.GetComponent<BoxCollider> ().enabled = false. $$anonymous$$eep in $$anonymous$$d, GetComponent is a quite expensive operation and you better put all colliders in an array and only then deactivate array objects (`cols [0].enabled = false`).

avatar image AurimasBlazulionis Eagle-Bound · Sep 17, 2016 at 05:36 AM 0
Share

Oh, and if you want to remove completely a component, then call Destroy (gameObject.GetComponent<BoxCollider> ()). Of, course, change gameObject with the object you want to do this.

avatar image Eagle-Bound AurimasBlazulionis · Sep 18, 2016 at 02:29 PM 0
Share

Thanks for the answer!

avatar image
0

Answer by Zlorak · Sep 16, 2016 at 11:43 AM

To activate/deactivate objects in the scene through script there is

 objectToActivate.gameObject.SetActive(true);
 objectToActivate.gameObject.SetActive(false);

Be aware that the GameObject has to be created first in the script to link it in Unity with:

 public GameObject objectName;

Example:

 using UnityEngine;
 using System;
 
 public class RedButton : MonoBehaviour {
 
     public GameObject button;
 
 void OnMouseOver(){
         button.gameObject.SetActive (false);
 }
 
 void OnMouseExit(){
         button.gameObject.SetActive (true);
 }
 }


Comment
Add comment · Show 2 · 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 Eagle-Bound · Sep 16, 2016 at 07:57 PM 0
Share

Thanks! do you think there would be a way to just remove a specific part of the game object? like the box collider or the audio source?

avatar image Zlorak Eagle-Bound · Sep 17, 2016 at 03:29 AM 0
Share

Err.. well, I haven't tried to remove for an amount of time ('cus I understand remove as destroy) an audio source or anything audio-related but I think if you attach that audio as a component of an object you can easily remove the object or you can try pausing the audio though.

Remember that an object that is not active won't run any of its components.

Anyway I looked it up and i found this: Unity Forum - Destroy a component

I'm not sure if that is what you're looking for since I think that'll destroy the component forever while is the game is running.

Follow this Question

Answers Answers and Comments

223 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

Related Questions

Animation won't stop (Solved) 2 Answers

GameObject not looking at me.. 1 Answer

Array index out of range(C#) 1 Answer

collider.gameObject.GetComponent doesn't have correct values yet it has the correct name 0 Answers

Status Effect Help 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