- Home /
Disable Scripts only works for one of the many
Hey y'all i have this c# script here: using UnityEngine; using System.Collections;
public class DisablePlayerThings : MonoBehaviour { public GameObject Playeryo;
// Use this for initialization
void start () {
Playeryo = GameObject.FindWithTag("Player");
}
void OnLevelWasLoaded(int level) {
if (level == 1)
Playeryo.GetComponent().enabled = false; Playeryo.GetComponent().enabled = false; Playeryo.GetComponent().enabled = false; Playeryo.GetComponent().enabled = false;
if (level == 3)
Playeryo.GetComponent().enabled = true; Playeryo.GetComponent().enabled = true; Playeryo.GetComponent().enabled = true; Playeryo.GetComponent().enabled = true;
}
}
and it only does its job (to disable a script on scene 1 and reanable it on scene 3) for the first script (Player) does anyone know why this happens?
Answer by liamcary · Feb 27, 2013 at 06:06 AM
That doesn't even compile for me.. Are you using angled brackets that aren't being shown in your post? If not, you'll need to specify what type of component to get.
But to answer the question, according to this code the three last components you get should always be set to true when OnLevelWasLoaded is called. That's because you aren't grouping the instructions after your if statement, meaning only the first instruction will rely on the if statement being true. You need:
if(level == 1)
{
// instruction1
// instruction2
// instruction3
}
instead of
if(level == 1)
// instruction1
//instruction2
//instruction3
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Multi similar script Components same game object 1 Answer
Disabling scripts 1 Answer
Distribute terrain in zones 3 Answers
I'm having more problems accessing a variable from another script in c# 2 Answers