Перейти к содержимому

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

  • автор:

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#?

Ibrahim Munaser's user avatar

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:

Unity 3d: Как отключить у объекта скрипт через другой скрипт?

Вы можете использовать функцию gameObject.GetComponent<T>().enabled = false;, где T — тип скрипта, который вы хотите отключить. Например:

MyScript scriptToDisable = gameObject.GetComponent<MyScript>();
scriptToDisable.enabled = false;

Включить скрипт с помощью этого способа можно так:
MyScript scriptToEnable = gameObject.GetComponent<MyScript>();
scriptToEnable.enabled = true;

Если вам нужно выключить или включить у другого объекта ваш скрипт
<<MyScript>> делайте следующие:

public GameObject MyObject;

MyScript scriptToEnable = MyObject.GetComponent<MyScript>();

MyObject- ваш объект на котором весит скрипт который вы отключаете или включаете. И не забудьте в инспекторе указать MyObject.

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: Advertisements

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

GameObject.SetActive

Activates/Deactivates the GameObject, depending on the given true or false value.

A GameObject may be inactive because a parent is not active. In that case, calling SetActive will not activate it, but only set the local state of the GameObject, which you can check using GameObject.activeSelf. Unity can then use this state when all parents become active.

Deactivating a GameObject disables each component, including attached renderers, colliders, rigidbodies, and scripts. For example, Unity will no longer call the Update() method of a script attached to a deactivated GameObject. OnEnable or OnDisable are called as the GameObject received SetActive(true) or SetActive(false) .

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *