0

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");

    }
}

}

2
  • What error do you get? Commented Mar 24, 2018 at 16:56
  • It's either GetComponent(typeof(BasketballSceneChange)) or GetComponent<BasketballSceneChange>() - it's clearly highlighted in the examples in the documentation: docs.unity3d.com/ScriptReference/GameObject.GetComponent.html Commented Mar 24, 2018 at 16:58

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

No, GetComponent(BasketballSceneChange) will not compile
@UnholySheep check my edit in my response you must write .GetComponent<BasketballSceneChange>().enabled
1

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.