- Home /
How To Disable, Not Deactivate All GameObjects In Array
Hi there,
Well, I have spent 4 hours tonight researching and reading Unity Answers to try and find a solution to a SIMPLE problem. Maybe my head is just too stuffed with information today.
I have an array of GameObjects which I access a script attached to each one of those gameobjects. These scripts have animations attached to them.
I want to be able to access the script on a button click, and on the same button click disable the gameobject. But, I want the script to continue playing after the button click, just do nothing upon a second button click. However the button has to remain active for all new objects added to the array.
using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour {
GameObject[] bombs;
GameObject[] explosions;
private InventoryControl inventoryControl;
void Start ()
{
GameObject inventoryControllerObject = GameObject.FindGameObjectWithTag ("GameController");
inventoryControl = inventoryControllerObject.GetComponent<InventoryControl> ();
}
void Update ()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 5)) {
Quaternion q = Quaternion.FromToRotation(transform.up, hit.normal);
Instantiate (inventoryControl.activeBomb, hit.point+hit.normal*0.01f, q);
}
}
}
public void explodeBomb()
{
bombs = GameObject.FindGameObjectsWithTag ("Bombs");
explosions = GameObject.FindGameObjectsWithTag ("Explosions");
for (int i=0; i<explosions.Length; i++)
{
explosions [i].GetComponent<Detonator> ().Explode ();
foreach (GameObject Explosions in explosions)
{
//I know something goes here to do what I want!!!1
}
for(int i=0; i<bombs.Length; i++)
{
Destroy(bombs[i]);
}
}
}
I'm losing my mind! It seems like this should be easy.
As always, you guys are awesome, and I know someone smarter than me will have a quick solution.
Thanks,
Cnote
Answer by Tanshaydar · Oct 24, 2014 at 08:16 AM
If I understand right, you don't want to see the gameobject, but want the script to continue executing.
If you deactivate a gameobject, all of its components will be disabled. You can, but, access each component individually and disable them one by one (unless they require each other, like joints require rigidbody).
If you want script to continue execute but don't want to see the object you can disable its mesh renderer. Furthermore, if the object has collider and you don't want it to affect the game anymore, you can access collider and disable it too.
Hi, sort of. I want the gameobject to continue it's already activated script, but I don't want it to receive input, as in button clicks, etc.
Well, you can include something like
public bool objectIsNotExlpoded;
if( objectIsNotExlpoded){
// all Update and FixedUpdate input checks here respectively
}
then set objectIsNotExlpoded
to false.
Hey there Tanshaydar, the answer I was looking for was your comment to my comment to your question. I knew it was something simple I was missing! haha
Your answer
Follow this Question
Related Questions
How to return index of List element? 1 Answer
How to store and iterate through a lot of Vector2s? 2 Answers
How many audio clip arrays are supported? 1 Answer
Get all GameObjects by variable value 2 Answers
Variable not accesible 2 Answers