Alright, I am working on a project that requires in game switching between three first person controllers. So basically what I need to do is to disable all of the first person controllers scripts and enable the ai scripts on the two unused characters and then do the opposite for the active character. An in game dummy object called world settings is used to keep track of what character the player is with the variable SelCharID. It sounded simple enough when I started. Here is the code I came up with.
Party Movement Script
var TEST:float;
var Player1:Transform;
var Player2:Transform;
var Player3:Transform;
var WorldSettings:Transform;
function Update () {
TEST = WorldSettings.SelCharID;
}
CharacterSwitchingScript
var Player1Cam:Transform;
var Player2Cam:Transform;
var Player3Cam:Transform;
public static var SelCharID:int = 1;
function Start () {
Player1.GetComponent(FPSInputController).enabled = true;
Player2.GetComponent(FPSInputController).enabled = false;
Player3.GetComponent(FPSInputController).enabled = false;
Player1.GetComponent(CharacterController).enabled = true;
Player2.GetComponent(CharacterController).enabled = false;
Player3.GetComponent(CharacterController).enabled = false;
Player1.GetComponent(CharacterMotor).enabled = true;
Player2.GetComponent(CharacterMotor).enabled = false;
Player3.GetComponent(CharacterMotor).enabled = false;
Player1.GetComponent(MouseLook).enabled = true;
Player2.GetComponent(MouseLook).enabled = false;
Player3.GetComponent(MouseLook).enabled = false;
Player1.GetComponent(MeleeSystem).enabled = true;
Player2.GetComponent(MeleeSystem).enabled = false;
Player3.GetComponent(MeleeSystem).enabled = false;
Player1.GetComponent(PartyMovement).enabled = false;
Player2.GetComponent(PartyMovement).enabled = true;
Player3.GetComponent(PartyMovement).enabled = true;
Player1Cam.GetComponent(Camera).enabled = true;
Player2Cam.GetComponent(Camera).enabled = false;
Player3Cam.GetComponent(Camera).enabled = false;
}
function Update () {
if (Input.GetKeyUp ("left shift"))
{
SelCharID++;
if(SelCharID>3)
{
SelCharID = 1;
}
if(SelCharID == 1)
{
Player1.GetComponent(FPSInputController).enabled = true;
Player2.GetComponent(FPSInputController).enabled = false;
Player3.GetComponent(FPSInputController).enabled = false;
Player1.GetComponent(CharacterController).enabled = true;
Player2.GetComponent(CharacterController).enabled = false;
Player3.GetComponent(CharacterController).enabled = false;
Player1.GetComponent(CharacterMotor).enabled = true;
Player2.GetComponent(CharacterMotor).enabled = false;
Player3.GetComponent(CharacterMotor).enabled = false;
Player1.GetComponent(MouseLook).enabled = true;
Player2.GetComponent(MouseLook).enabled = false;
Player3.GetComponent(MouseLook).enabled = false;
Player1.GetComponent(MeleeSystem).enabled = true;
Player2.GetComponent(MeleeSystem).enabled = false;
Player3.GetComponent(MeleeSystem).enabled = false;
Player1.GetComponent(PartyMovement).enabled = false;
Player2.GetComponent(PartyMovement).enabled = true;
Player3.GetComponent(PartyMovement).enabled = true;
Player1Cam.GetComponent(Camera).enabled = true;
Player2Cam.GetComponent(Camera).enabled = false;
Player3Cam.GetComponent(Camera).enabled = false;
}
if(SelCharID == 2)
{
Player1.GetComponent(FPSInputController).enabled = false;
Player2.GetComponent(FPSInputController).enabled = true;
Player3.GetComponent(FPSInputController).enabled = false;
Player1.GetComponent(CharacterController).enabled = false;
Player2.GetComponent(CharacterController).enabled = true;
Player3.GetComponent(CharacterController).enabled = false;
Player1.GetComponent(CharacterMotor).enabled = false;
Player2.GetComponent(CharacterMotor).enabled = true;
Player3.GetComponent(CharacterMotor).enabled = false;
Player1.GetComponent(MouseLook).enabled = false;
Player2.GetComponent(MouseLook).enabled = true;
Player3.GetComponent(MouseLook).enabled = false;
Player1.GetComponent(MeleeSystem).enabled = false;
Player2.GetComponent(MeleeSystem).enabled = true;
Player3.GetComponent(MeleeSystem).enabled = false;
Player1.GetComponent(PartyMovement).enabled = true;
Player2.GetComponent(PartyMovement).enabled = false;
Player3.GetComponent(PartyMovement).enabled = true;
Player1Cam.GetComponent(Camera).enabled = false;
Player2Cam.GetComponent(Camera).enabled = true;
Player3Cam.GetComponent(Camera).enabled = false;
}
if(SelCharID == 3)
{
Player1.GetComponent(FPSInputController).enabled = false;
Player2.GetComponent(FPSInputController).enabled = false;
Player3.GetComponent(FPSInputController).enabled = true;
Player1.GetComponent(CharacterController).enabled = false;
Player2.GetComponent(CharacterController).enabled = false;
Player3.GetComponent(CharacterController).enabled = true;
Player1.GetComponent(CharacterMotor).enabled = false;
Player2.GetComponent(CharacterMotor).enabled = false;
Player3.GetComponent(CharacterMotor).enabled = true;
Player1.GetComponent(MouseLook).enabled = false;
Player2.GetComponent(MouseLook).enabled = false;
Player3.GetComponent(MouseLook).enabled = true;
Player1.GetComponent(MeleeSystem).enabled = false;
Player2.GetComponent(MeleeSystem).enabled = false;
Player3.GetComponent(MeleeSystem).enabled = true;
Player1.GetComponent(PartyMovement).enabled = true;
Player2.GetComponent(PartyMovement).enabled = true;
Player3.GetComponent(PartyMovement).enabled = false;
Player1Cam.GetComponent(Camera).enabled = false;
Player2Cam.GetComponent(Camera).enabled = false;
Player3Cam.GetComponent(Camera).enabled = true;
}
}
}
I’m not getting any compiler errors,but whenever I run the game the characters do indeed switch and I can see all of the scripts activating/deactivating in the editor. But the TEST variable from the ai script is not updating. Also, The active player works perfectly but even with all of the scripts disabled the inactive players are still looking the same direction as the player, but not moving or rotating with it. (This makes me think its something to do with the mouse look script). ANY help anyone could offer with this would be greatly appreciated
austin_farmer90:
Alright, I am working on a project that requires in game switching between three first person controllers. So basically what I need to do is to disable all of the first person controllers scripts and enable the ai scripts on the two unused characters and then do the opposite for the active character. An in game dummy object called world settings is used to keep track of what character the player is with the variable SelCharID. It sounded simple enough when I started. Here is the code I came up with.
Party Movement Script
var TEST:float;
var Player1:Transform;
var Player2:Transform;
var Player3:Transform;
var WorldSettings:Transform;
function Update () {
TEST = WorldSettings.SelCharID;
}
CharacterSwitchingScript
var Player1Cam:Transform;
var Player2Cam:Transform;
var Player3Cam:Transform;
public static var SelCharID:int = 1;
function Start () {
Player1.GetComponent(FPSInputController).enabled = true;
Player2.GetComponent(FPSInputController).enabled = false;
Player3.GetComponent(FPSInputController).enabled = false;
Player1.GetComponent(CharacterController).enabled = true;
Player2.GetComponent(CharacterController).enabled = false;
Player3.GetComponent(CharacterController).enabled = false;
Player1.GetComponent(CharacterMotor).enabled = true;
Player2.GetComponent(CharacterMotor).enabled = false;
Player3.GetComponent(CharacterMotor).enabled = false;
Player1.GetComponent(MouseLook).enabled = true;
Player2.GetComponent(MouseLook).enabled = false;
Player3.GetComponent(MouseLook).enabled = false;
Player1.GetComponent(MeleeSystem).enabled = true;
Player2.GetComponent(MeleeSystem).enabled = false;
Player3.GetComponent(MeleeSystem).enabled = false;
Player1.GetComponent(PartyMovement).enabled = false;
Player2.GetComponent(PartyMovement).enabled = true;
Player3.GetComponent(PartyMovement).enabled = true;
Player1Cam.GetComponent(Camera).enabled = true;
Player2Cam.GetComponent(Camera).enabled = false;
Player3Cam.GetComponent(Camera).enabled = false;
}
function Update () {
if (Input.GetKeyUp ("left shift"))
{
SelCharID++;
if(SelCharID>3)
{
SelCharID = 1;
}
if(SelCharID == 1)
{
Player1.GetComponent(FPSInputController).enabled = true;
Player2.GetComponent(FPSInputController).enabled = false;
Player3.GetComponent(FPSInputController).enabled = false;
Player1.GetComponent(CharacterController).enabled = true;
Player2.GetComponent(CharacterController).enabled = false;
Player3.GetComponent(CharacterController).enabled = false;
Player1.GetComponent(CharacterMotor).enabled = true;
Player2.GetComponent(CharacterMotor).enabled = false;
Player3.GetComponent(CharacterMotor).enabled = false;
Player1.GetComponent(MouseLook).enabled = true;
Player2.GetComponent(MouseLook).enabled = false;
Player3.GetComponent(MouseLook).enabled = false;
Player1.GetComponent(MeleeSystem).enabled = true;
Player2.GetComponent(MeleeSystem).enabled = false;
Player3.GetComponent(MeleeSystem).enabled = false;
Player1.GetComponent(PartyMovement).enabled = false;
Player2.GetComponent(PartyMovement).enabled = true;
Player3.GetComponent(PartyMovement).enabled = true;
Player1Cam.GetComponent(Camera).enabled = true;
Player2Cam.GetComponent(Camera).enabled = false;
Player3Cam.GetComponent(Camera).enabled = false;
}
if(SelCharID == 2)
{
Player1.GetComponent(FPSInputController).enabled = false;
Player2.GetComponent(FPSInputController).enabled = true;
Player3.GetComponent(FPSInputController).enabled = false;
Player1.GetComponent(CharacterController).enabled = false;
Player2.GetComponent(CharacterController).enabled = true;
Player3.GetComponent(CharacterController).enabled = false;
Player1.GetComponent(CharacterMotor).enabled = false;
Player2.GetComponent(CharacterMotor).enabled = true;
Player3.GetComponent(CharacterMotor).enabled = false;
Player1.GetComponent(MouseLook).enabled = false;
Player2.GetComponent(MouseLook).enabled = true;
Player3.GetComponent(MouseLook).enabled = false;
Player1.GetComponent(MeleeSystem).enabled = false;
Player2.GetComponent(MeleeSystem).enabled = true;
Player3.GetComponent(MeleeSystem).enabled = false;
Player1.GetComponent(PartyMovement).enabled = true;
Player2.GetComponent(PartyMovement).enabled = false;
Player3.GetComponent(PartyMovement).enabled = true;
Player1Cam.GetComponent(Camera).enabled = false;
Player2Cam.GetComponent(Camera).enabled = true;
Player3Cam.GetComponent(Camera).enabled = false;
}
if(SelCharID == 3)
{
Player1.GetComponent(FPSInputController).enabled = false;
Player2.GetComponent(FPSInputController).enabled = false;
Player3.GetComponent(FPSInputController).enabled = true;
Player1.GetComponent(CharacterController).enabled = false;
Player2.GetComponent(CharacterController).enabled = false;
Player3.GetComponent(CharacterController).enabled = true;
Player1.GetComponent(CharacterMotor).enabled = false;
Player2.GetComponent(CharacterMotor).enabled = false;
Player3.GetComponent(CharacterMotor).enabled = true;
Player1.GetComponent(MouseLook).enabled = false;
Player2.GetComponent(MouseLook).enabled = false;
Player3.GetComponent(MouseLook).enabled = true;
Player1.GetComponent(MeleeSystem).enabled = false;
Player2.GetComponent(MeleeSystem).enabled = false;
Player3.GetComponent(MeleeSystem).enabled = true;
Player1.GetComponent(PartyMovement).enabled = true;
Player2.GetComponent(PartyMovement).enabled = true;
Player3.GetComponent(PartyMovement).enabled = false;
Player1Cam.GetComponent(Camera).enabled = false;
Player2Cam.GetComponent(Camera).enabled = false;
Player3Cam.GetComponent(Camera).enabled = true;
}
}
}
I’m not getting any compiler errors,but whenever I run the game the characters do indeed switch and I can see all of the scripts activating/deactivating in the editor. But the TEST variable from the ai script is not updating. Also, The active player works perfectly but even with all of the scripts disabled the inactive players are still looking the same direction as the player, but not moving or rotating with it. (This makes me think its something to do with the mouse look script). ANY help anyone could offer with this would be greatly appreciated
I’m not a java expert, but I’m pretty sure you need to make your variables public to access them. They’ll be private by default.
public var TEST:float
You can’t access variables from other scripts unless they are set to public or static.
Thanks for the reply. I tried what you said but its still not working though.
oh, look at that gorgeous copy and past code design. isnt it wonderfull /sarcasm
I wasn’t going to open that can of worms lol.
There are a few mistakes and they are so simple so lets start.
var TEST:float;
var Player1:Transform;
var Player2:Transform;
var Player3:Transform;
var WorldSettings:Transform;
function Update () {
TEST = WorldSettings.SelCharID;
}
WorldSettings is a Transform not your custom script. I am assuming WorldSettings is a GameObject assigned in the inspector. Also TEST is a float and you are getting an int from SelCharID. So based on these assumptions and i am not guaranteeing it would work your script should be like so:
var SelectedCharacterID:int;
var Player1:Transform;
var Player2:Transform;
var Player3:Transform;
var WorldSettings:Transform;
var characterSwitchingScript:CharacterSwitchingScript; //Notice lowercase c in character.
function Start() {
characterSwitchingScript = WorldSettings.GetComponent(CharacterSwitchingScript);
SelectedCharacterID = characterSwitchingScript.SelCharID;
Debug.Log(SelectedCharacterID);
}
Hope this helps and please read some tutorials or at least the Unity reference.
Thanks MeanCoder that worked