The question is answered, right answer was accepted
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
}
}
}
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.
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?
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`).
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.
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);
}
}
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?
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
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