Hey.
i have several scripts running in a screen. when i press play i need to be able to control which script is running and which is not. is there a “easy” way to accomplish this?
this script is attached to my player. i dont know if that’s necessary but i’ll post that too, and want that script to turn off all scripts that i choose.
#pragma strict
function OnMouseDown () {
GetComponent.().isKinematic = false;
Cursor.visible = false;
}
Happy easter!
Yes, the easy way is to turn off a script via the .enabled bool of your script, which is inherited from MonoBehaviour. If your script includes the Start() function, it will also show a checkbox in the inspector. This represents the .enabled field which is automatically in every component that has a Start() function. Just find a reference to a script and then set script.enabled = false;
Thanks Xarbrough.
I am afraid i am too new to this to understand that you wrote
But i will try to implement that you said in the script.
I can’t see what the script you posted should do, so I’m just explaining how to disable a script:
using UnityEngine;
using System.Collections;
public class MainScript : MonoBehaviour {
void Start () {
//Find a reference to your scripts
DragDrop dragDropScript = Object.FindObjectOfType<DragDrop>();
Rotation rotationScript = Object.FindObjectOfType<Rotation>();
//FindObjectOfType is very slow and you need to hardcode the class name
//Alternatively:
//Player playerScript = GameObject.FindGameObjectWithTag("Player");
//dragDropScript = playerScript.gameObject.Getcomponent<DragDrop>();
//Or if all scripts are on the same gameobjects
//dragDropScript = GetComponent<DragDrop>();
TurnOffScript(dragDropScript);
TurnOffScript(rotationScript);
}
void TurnOffScript (MonoBehaviour script) {
if(script != null)
script.enabled = false;
}
}
In any case, especially if you don’t know what a reference is, or what the .enabled property means, you can watch this official Unity tutorial: http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/coding-for-the-absolute-beginner
1 Like
Thanks Yarbrough!
i test this as soon as i get home. i really appreciate your help,
Yes got it working!
Thank you so much for all your help mr Xarbrough. i really appreciate it.