This worked briefly yesterday, I believe I did something to mess this up. All my inputs that I put in are correct; therefor, it must be the code. I want to switch players. While the red cube's script is enabled and can move under the movement script I want the blue cubes movement script to be disabled and vice versus. Can you give me some insight into what I'm doing wrong? Please and thank you!!
using UnityEngine;
using System.Collections;
public class PlayerMovementScript : MonoBehaviour {
public float speed = 2f;
public float height = 2f;
public Component playerMovement;
public GameObject redCube;
public GameObject blueCube;
void Start()
{
var bc = blueCube.GetComponent<PlayerMovementScript>();
bc.enabled = !bc.enabled;
}
// Update is called once per frame
void Update ()
{
if (Input.GetButton ("Right"))
{
transform.Translate(Vector3.right * speed * Time.deltaTime);
}
if (Input.GetButton ("Left"))
{
transform.Translate(Vector3.left * speed * Time.deltaTime);
}
if (Input.GetButton ("Jump"))
{
transform.Translate(Vector3.up * height * Time.deltaTime);
}
var bc = blueCube.GetComponent<PlayerMovementScript>();
var rc = redCube.GetComponent<PlayerMovementScript>();
if (Input.GetButton ("Switch"))
{
bc.enabled = !bc.isActiveAndEnabled;
rc.enabled = !rc.enabled;
}
}
}
isActive
andisEnabled
were booleans underGameObject
(IIRC); this implementation uses the component type (note that the cubes are stored as script references, I.e. components, not game objects. Also note thatisActive
andisEnabled
have both been deprecated and removed. You won't find these variables any more, when reading the current API. \$\endgroup\$