Disable a C# script from another C# script.
Hello! have a "Press S To Start" Script (Special thanks to @Mavina) But I also need to disable a script from that script. I have played around with the enabled property and but can't seem to get it to work. Here is what I have so far...
using UnityEngine;
using System.Collections;
public class WaitForPlayer : MonoBehaviour
{
public GameObject waitScreen;
public GameObject mainPlayer;
public Component mainScript;
// flag to determine if we are waiting for uset input to start game
private bool waitingToStartGame = true;
// Use this for initialization
void Start ()
{
if (waitScreen != null)
{
waitScreen.SetActive(true);
}
else
{
waitingToStartGame = false;
Debug.LogError("waitScreen was not set in the inspector. Please set and try again");
}
if (mainPlayer != null)
{
mainPlayer.SetActive(false);
mainScript.enabled (false);
}
else
{
Debug.LogError("mainPlayer was not set in the inspector. Please set and try again");
}
}
void Update ()
{
// if the waitingToStartGame is enabled and the 'S' key has been pressed
if (waitingToStartGame && (Input.GetKeyDown(KeyCode.S)))
{
// set the flag to false so that will no longer be checking for input to start game
waitingToStartGame = false;
if (waitScreen != null)
{
waitScreen.SetActive(false);
}
if (mainPlayer != null)
{
mainPlayer.SetActive(true);
mainScript.enabled (true);
}
}
}
}
Thanks! :D
Answer by TBruce · Oct 14, 2016 at 08:22 PM
Try this
using UnityEngine;
using System.Collections;
public class WaitForPlayer : MonoBehaviour
{
public GameObject waitScreen;
public GameObject mainPlayer;
// at a minimum the type needs to be Behaviour to get access to the variable 'enabled' but best practice is to use the actual type - e.g. MainScript
public Behaviour mainScript;
// flag to determine if we are waiting for uset input to start game
private bool waitingToStartGame = true;
// Use this for initialization
void Start ()
{
if (waitScreen != null)
{
waitScreen.SetActive(true);
}
else
{
waitingToStartGame = false;
Debug.LogError("waitScreen was not set in the inspector. Please set and try again");
}
if (mainPlayer != null)
{
mainPlayer.SetActive(false);
// note: mainScript can not be on the same game object as mainPlayer, if it is then place the following above mainPlayer
if (mainScript != null)
{
mainScript.enabled = false;
}
}
else
{
Debug.LogError("mainPlayer was not set in the inspector. Please set and try again");
}
}
void Update ()
{
// if the waitingToStartGame is enabled and the 'S' key has been pressed
if (waitingToStartGame && (Input.GetKeyDown(KeyCode.S)))
{
// set the flag to false so that will no longer be checking for input to start game
waitingToStartGame = false;
if (waitScreen != null)
{
waitScreen.SetActive(false);
}
if (mainPlayer != null)
{
mainPlayer.SetActive(true);
if (mainScript != null)
{
mainScript.enabled = true;
}
}
}
}
}
You should change this
public Component mainScript;
to something like this
public MainScript mainScript;
where MainScript
is the actual class name.
Hi! Thank you again for your valuable help! I will not get to it today but hopefully in the next couple days and when I do I will comment with feedback. Cheers :D
Ok, so I gave it a go and I had to rearrange the if (mainScript != null) part because the script was in the mainPlayer. I am getting the error CS0246: The type or namespace name `$$anonymous$$ainScript' could not be found. Are you missing a using directive or an assembly reference?. Thanks again for your help :D
Your original code had
public Component mainScript;
As I stated above you need to change Component
to the actual class name for mainScript
. I do not know what it is but since you called the variable mainScript
I just set the class name to $$anonymous$$ainScript
. But you need to change the code to match whatever the class is named.
The main reason that you can not use
public Component mainScript;
is that Component
does not have a variable called enabled
. At a $$anonymous$$imum the type needs to be Behaviour to get access to the variable 'enabled' like so
public Behaviour mainScript;
but best practice is to use the actual type - e.g. $$anonymous$$ainScript (I could only assume what the actual class name is going by the variable name provided). Using Behaviour will only limit your use of what you can do with mainScript
.
Thanks for your help and sorry for my delayed response. The script I am trying to disable and enable is a C# script (mainScript) in the $$anonymous$$ain Player. So while the game is waiting on the "press S to start" screen the timer in mainScript is stopped. I am not sure what class name to use for that to work. Thanks Again!
Your answer
Follow this Question
Related Questions
On Renderer Was Enabled? (C#) 0 Answers
Only Disable One Update on a Script 1 Answer
Why isnt the enabled property part of an interface? 0 Answers
Does the amount of if else statements within the Update matter? 0 Answers
NullPointException Object not found, caused by a variable not accepting value? 0 Answers