Enable a disabled gameobject via javascript?

Hi all. I am trying to build a simple system for entering and exiting vehicles. Currently both the player and the vehicle are the default FPS controllers. I am able to run up to the vehicle controller, enter it, disable the player controller, and drive the vehicle. However, I cannot re-enable the disabled player controller. Any and all help is appreciated. Here is the code I am using, it is applied to the vehicle, which has a trigger for range sensing.
Here is my code


#pragma strict

var inRange = false;
var inCar = false;
//This is the player that enters the vehicle:
var player : Transform;
//This is the player camera:
var playerCamera : Camera;
//This is the Vehicle:
var Vehicle : Transform;
//This is the vehicle camera:
var vehicleCamera : Camera;
//These are the control scripts for your vehicle:
private var lookScript : MouseLook;
private var moveScript : CharacterMotor;
private var inputScript : FPSInputController;

function Start () {
	playerCamera.enabled = true;
	vehicleCamera.enabled = false;
        //These are the vehicle control scripts.
	lookScript = GetComponent(MouseLook);
	moveScript = GetComponent(CharacterMotor);
	inputScript = GetComponent(FPSInputController);
}

function Update () {
	if(Input.GetButton("Enter") && inRange == true) {
		if(inCar == false) {
			inCar = true;
			//Switch Cameras
			playerCamera.enabled = false;
			vehicleCamera.enabled = true;
			//Parent player to Vehicle
			player.parent = Vehicle.transform;
			player.transform.localPosition = Vector3(-1.5,0,0);
			//Make Player Invisible
			player.gameObject.SetActive(false);
			//Enable Vehicle Control Scripts
			lookScript.enabled = !lookScript.enabled;
			moveScript.enabled = !moveScript.enabled;
			inputScript.enabled = !inputScript.enabled;
		}
	}
	else if(Input.GetButton("Enter") && inCar == true) {
		//inCar = false;
		//Switch Cameras
		playerCamera.enabled = true;
		vehicleCamera.enabled = false;
		//Unparent player to Vehicle
		player.transform.parent = null;
		//Make Player Visible
		player.gameObject.SetActive(true);
		//Enable Vehicle Control Scripts
		lookScript.enabled = !lookScript.enabled;
		moveScript.enabled = !moveScript.enabled;
		inputScript.enabled = !inputScript.enabled;
	}	
}
function OnTriggerEnter (other : Collider) {
        inRange = true;
        Debug.Log("InRange!");
}
function OnTriggerExit (other : Collider) {
		inRange = false;
}

If anyone can help me figure this out, that would be amazing. Thank you in advance.

So close to getting a badge...

1 Answer

1

Do NOT use this format

lookScript.enabled = !lookScript.enabled;

just set them explicitly:

lookScript.enabled = false; (or whatever the case is)

in each of the six lines of code like that.

@Fattie, a little off topic but, how would I setActive a child GO, the becomes inactive at the start, but I want to become active again after a collision event?

nowadays sit is SetActive(false); and SetActive(true); so just add a one-line script TO the child object that sets it not active, in Start() in the PARENT script, have var theChildOfInterest:GameObject and connect it to that. after the collision (or whenever you want) just set it active, theChildOfInterest.SetActive .. etc.

YES!! I got it to work, thank you. :D

lol GREAT !!