1
\$\begingroup\$

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;
        }
    }   
}
\$\endgroup\$
6
  • 1
    \$\begingroup\$ What is your issue and what are you trying to achieve? \$\endgroup\$
    – Vaillancourt
    Commented Jul 22, 2015 at 12:36
  • \$\begingroup\$ Exactly, what's current behavior and what you aim at? \$\endgroup\$
    – Leggy7
    Commented Jul 22, 2015 at 12:43
  • \$\begingroup\$ @AlexandreVaillancourt and Leggy7 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. \$\endgroup\$
    – Ham
    Commented Jul 22, 2015 at 12:56
  • \$\begingroup\$ it sounds to me like you need to read on the isActive and isEnabled flags in the Unity docs \$\endgroup\$
    – Stephan
    Commented Jul 22, 2015 at 21:03
  • \$\begingroup\$ @Stephan, this is a case of alternate implementation, not incorrect implementation. isActive and isEnabled were booleans under GameObject (IIRC); this implementation uses the component type (note that the cubes are stored as script references, I.e. components, not game objects. Also note that isActive and isEnabled have both been deprecated and removed. You won't find these variables any more, when reading the current API. \$\endgroup\$
    – Gnemlock
    Commented Dec 27, 2016 at 21:47

1 Answer 1

1
\$\begingroup\$

I eliminated the script's start function and just called the components in the update function.

var bc = blueCube.GetComponent<PlayerMovementScript>();
        var rc = redCube.GetComponent<PlayerMovementScript>();

        if (Input.GetButton ("Switch")) 
        {
            bc.enabled = !bc.enabled;
            rc.enabled = !rc.enabled;
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Please note that you are not really doing anything different, of we can assume your Start method calls (if it does not, that's an entirely different problem, and an entirely different question). This suggests that removing bc.enabled = !bc.enabled from Start, and copying this line over bc.enabled = BC.isActiveAndEnabled in your "Switch" button solved the problem. This is helpful in knowing why it happened in the first place. Please mark this as the accepted answer, to let others know this worked. \$\endgroup\$
    – Gnemlock
    Commented Dec 27, 2016 at 21:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .