Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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;
        }
    }   
}
share|improve this question
1  
What is your issue and what are you trying to achieve? – Alexandre Vaillancourt Jul 22 '15 at 12:36
    
Exactly, what's current behavior and what you aim at? – Leggy7 Jul 22 '15 at 12:43
    
@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. – Ham Jul 22 '15 at 12:56
    
it sounds to me like you need to read on the isActive and isEnabled flags in the Unity docs – Stephan Jul 22 '15 at 21:03

As you said it was working fine, i found this thing different than other inside Update function:

bc.enabled = !bc.isActiveAndEnabled;

Is this logic should works on isActiveAndEnabled instead of enabled ?

share|improve this answer

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;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.