- Home /
How to disable script ,How to disable a script in multiple Gameobjects from another script?
Hello. How can I disable script in another gameobject? I am making a physic game, where you must drag elements, like triangles to get ball to the basket. After you drag elements, you can press "StartPhysic" button to make all draggable elements dynamic. How can I disable "MouseDragScript" in all elements after pressing "StartPhysic" button? I tried my best with just one component, unity doesn't show any errors but it obviously doesn't work. Here is my "StartPhysic" code. Thanks.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StartPhysic : MonoBehaviour
{
public Rigidbody2D[] rBodies;
public GameObject element;
// Start is called before the first frame update
public bool phisicDisabled = true;
//Player.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Static;
public void OnMouseUp()
{
foreach (Rigidbody2D item in rBodies)
{
item.bodyType = RigidbodyType2D.Dynamic;
}
element.GetComponent<MouseDrag>().enabled = false;
}
}
Answer by tormentoarmagedoom · Feb 02, 2020 at 06:07 PM
Hello There.
Easiest way is to have all the objects inside a array or inside a list. And be sure al objects in that list/array have the component.
Then just do something like:
foreach(GameObject Element in ListOfelements)
{
Element.GetComponent<TheScript>().enable=false;
}
And the same to enable them again.
Another way to find all that objects, is to use the same Tag (like "Triangle")and create the array every time, like
ListOfelements = GameObject.FindGameObjectsWithTag("Triangle")
Bye.
Your answer
![](https://koobas.hobune.stream/wayback/20220612222556im_/https://answers.unity.com/themes/thub/images/avi.jpg)