Abdulhamid
Abdulhamid

Reputation: 325

Disable a script when another script is active

So I have got two scene change scripts, both which are attached to two different game objects. But for some reason, one script affects both game objects. So I am trying to temporarily disable one of the scripts when the other script is active on the other game object. Here is what I have but this is returning the error, "BasketballSceneChange' is a type, which is not valid in the given context".

    **GameObject.Find("pPrism1").GetComponent(BasketballSceneChange).enabled = false;**
{
    if (Input.GetMouseButtonDown(0) && SceneManager.GetActiveScene().name == "MWalk")
    {
        SceneManager.LoadScene("BWalk");

    }


    if (Input.GetMouseButton(0) && SceneManager.GetActiveScene().name == "BWalk")
    {
        SceneManager.LoadScene("Basketball");

    }

    if (Input.GetMouseButton(0) && SceneManager.GetActiveScene().name == "Football")
    {
        SceneManager.LoadScene("SWalk");

    }

    if (Input.GetMouseButton(0) && SceneManager.GetActiveScene().name == "SWalk")
    {
        SceneManager.LoadScene("MWalk");

    }
}

}

Upvotes: 0

Views: 218

Answers (2)

Chopi
Chopi

Reputation: 1143

You could avoid using GetComponent just declaring a public variable and assigning it from the inspector.

public AnotherScript theOtherScript;
void Update()
{
   if(theOtherScript != null && theOtherScript.enabled)
   {
      theOtherScript.enabled = false;
   }
}

Check this Tutorial

Other way is Finding the gameobject and geting the component as you did.

I've read your code again and I found the error that doesn't compile the line:

GameObject.Find("pPrism1").GetComponent(BasketballSceneChange).enabled = false;

should be

GameObject.Find("pPrism1").GetComponent<BasketballSceneChange>().enabled = false;

Upvotes: 1

sfarbac
sfarbac

Reputation: 145

Change this:

GameObject.Find("pPrism1").GetComponent(BasketballSceneChange).enabled = false;

to

 var myVariable = GetComponent(BasketballSceneChange) as myBaseClass;

 myVariable.enabled = false;

change myBaseClass to class which shows your Scene.

Upvotes: 2

Related Questions