Как отключить скрипт на объекте unity

от admin

unity How Do I disable current script or any other script in the program within code?

In script BOB, I want to enable ABE, and disable GUY. how could this be done in unity, using c#?

3 Answers 3

If you have attached all three scripts in same GameObject then I would say go with James Hogle answer.

If all scripts are not different GameObjects then to disable GUY in BOB you can use the following code in BOB

similarly you can use it to set true or make it enable

Hamza Hasan's user avatar

First you need to know the gameObject that the script you want to enable/disable is attached to. For example, if BOB, ABE and GUY are all on the same object, you could say in BOB’s code:

If the script you are trying to disable is on a different gameObject than you are going to have to get a hold of that object. See:

Or perhaps if you are dealing with collisions see:

James Hogle's user avatar

It’s better to avoid GameObject.Find in Unity as well as GameObject.GetComponent because of performance (especially if at runtime).

One better solution is to make both ABE and GUY static:

GUY.cs :

ABE.cs :

Now, since you the other scripts in this way (static), you can simply do this:

Читать:
Как узнать ширину блока js

Unity – Programmatically Enable/Disable a Script Component

This short and straight-to-the-post post shows you how to programmatically enable or disable of C# file in Unity (not using the checkbox in the Inspector panel).

Let’s say we have a C# file named Rocket that was attached to one of our game objects (the name of the game object doesn’t matter). If you want to temporarily detach it based on some conditions, you can do like this: Advertisements

To enable it again from the code:

When the currently active scene gets reloaded or a new scene is loaded, the C# script will automatically be turned on.

Как отключить скрипт на объекте unity

Андрей Псевдонимов

Андрей Псевдонимов

Эльбрус Кусаев

Андрей Псевдонимов

Андрей Псевдонимов ответил Эльбрусу
книжек почитать очевидно🌚

Жавохир Исаев

Жавохир Исаев

Эльбрус Кусаев

Вячеслав Сергеевич

Вячеслав Сергеевич

Леонид Пожидаев

Леонид Пожидаев

Как отключить скрипт на объекте unity

using UnityEngine;
using System.Collections;

public class FPSHandR : MonoBehaviour <
public Transform RHand;
private HandItem item;

public void addHand (HandItem it) <
if (item != null) <
item.transform.SetParent(null);
item.gameObject.AddComponent<Rigidbody>();
>
item = it;
it.transform.SetParent (RHand);
it.transform.localPosition = it.position;
it.transform.localRotation = Quaternion.Euler(it.rotation);
Destroy (it.GetComponent<Rigidbody>());
>
>

using UnityEngine;
using System.Collections;

public class FPSHandR : MonoBehaviour <
public Transform RHand;
private HandItem item;

Похожие статьи