on/off script problem

Hi,
I want to create two GUI buttons and with then switch between characters. I have two cubes(miner,architect) with attached and disabled grid_moving.js. Script enable or disable this script but when is disabled cube can still move. Can someone help?
Iam not very good in coding.

var player1 = false;

var player2 = false;



function OnGUI(){

if(GUI.Button(Rect(60, 0, 150, 20), "player 1")){

player1 = true;

player2 = false;



}

if(GUI.Button(Rect(60, 25, 150, 20), "player 2")){

player1 = false;

player2 = true;

}

}


function Update(){

if(player1 == true){

GameObject.Find("miner").GetComponent("grid_moving").enabled=true;

}

if(player2 == true){

GameObject.Find("architect").GetComponent("grid_moving").enabled=true;

}

}

thanks a lot for help

Your not disabling any of them your just enabling, when you enable one disable the other that might help.

you mean like this?

var player1 = false;

var player2 = false;



function OnGUI(){

if(GUI.Button(Rect(60, 0, 150, 20), "player 1")){

player1 = true;

player2 = false;

}

if(GUI.Button(Rect(60, 25, 150, 20), "player 2")){

player1 = false;

player2 = true;

}

}


function Update(){

if(player1 == true){

GameObject.Find("miner").GetComponent("grid_moving").enabled=true;

GameObject.Find("architect").GetComponent("grid_moving").enabled=false;

}

if(player2 == true){

GameObject.Find("miner").GetComponent("grid_moving").enabled=false;

GameObject.Find("architect").GetComponent("grid_moving").enabled=true;

}

}

this dont work too

Try just enabling / disabling the Game Object itself.

GameObject.Find(“architect”).enabled = false;

Or put a variable in you grid_moving script that tracks whether it is the currently controlled character?

“cubes” must be visible all the time som enable/disable GameObject is not good idea

its a fine idea :stuck_out_tongue: disabling an object in this way leaves it visible. If you want it to disappear / reappear you must use gameObject.renderer.enabled

:slight_smile: if object will be visible it is good idea
EDIT: I try it again it work BUT script work just once.
activate first - can move
activate second - can move
activate first again - cant move
I just can see how are gameobjects activate/deactivate in editor.

var player1 = false;

var player2 = false;

var miner : GameObject;

var architect : GameObject;



function OnGUI(){

if(GUI.Button(Rect(60, 0, 150, 20), "miner")){

player1 = true;

player2 = false;

}

if(GUI.Button(Rect(60, 25, 150, 20), "architect")){

player1 = false;

player2 = true;

}

}



function Update(){

if(player1 == true){

miner.active = true;

architect.active = false;

}

if(player2 == true){

miner.active = false;

architect.active = true;

}

}

no one? please help

Try this =/

function Update(){

if(player1 == true){

GameObject.Find("miner").GetComponent("grid_moving").enabled=true;
player2 = false;

}

if(player2 == true){

GameObject.Find("architect").GetComponent("grid_moving").enabled=true;

player1 = false;

}
}

when I select miner he is moving fine but when select architect second. both object are moving in one time = same as my first script

Try this one then. Not sure if it’ll change anything but give it a go.

function Update(){

if(player1 == true){
GameObject.Find(“miner”).GetComponent(“grid_moving”).enabled=true;

if(player2 == false)
{
GameObject.Find(“architect”).GetComponent(“grid_moving”).enabled=false;
}
}

if(player2 == true){
GameObject.Find(“architect”).GetComponent(“grid_moving”).enabled=true;

if(player1 == false)
{
GameObject.Find(“architect”).GetComponent(“grid_moving”).enabled=false;
}
}

}

I think this is because when you select miner in update function you enable it therefore it works fine,but when you
select architect while in update function you didn’t disable miner ,you enable architect and both object are moving in one time=because both are enable.
use this code instead previous update function:
////////////////////////////////////
function Update(){

if(player1 == true){

GameObject.Find(“miner”).GetComponent(“grid_moving”).enabled=true;
player2 = false;

}
if(player1 == false){

GameObject.Find(“miner”).GetComponent(“grid_moving”).enabled=false;

}

if(player2 == true){

GameObject.Find(“architect”).GetComponent(“grid_moving”).enabled=true;

player1 = false;

}
if(player2 == false){

GameObject.Find(“architect”).GetComponent(“grid_moving”).enabled=false;

}

}

///////////////////////////////////
hope this help!

@Castino it is the same:(
@masterjet your script deactivate grid_moving on miner when i select architect BUT weird thing is miner is still moving

Maybe there’s something going on somewhere then?

Hi matis!
I create a test scene with same script assigned to an empty gameobject and two third person character in scene
and assign variables and it work ok.no problem with activate/deactivate.
just do a little improvement:
replace by this:

private var player1 = false;

private var player2 = false;

@masterjet ok thanks a lot in new scene that work in my scene not but I look on that
Thanks again

@masterjet problem is with GridMove/grid_moving script

var walkSpeed : float = 1.0;

var runSpeed : float = 2.0;
var gridSize : int = 1;
 enum Orientation {Horizontal, Vertical}
var gridOrientation = Orientation.Horizontal;
var allowDiagonals = false;
var correctDiagonalSpeed = true;
private var input = Vector2.zero;

function Start () {
     var myTransform = transform;
     var startPosition : Vector3;
     var endPosition : Vector3;
     var t : float;
     var tx : float;
     var moveSpeed = walkSpeed;
     
     while (true) {
         while (input == Vector2.zero) {
             GetAxes();
             tx = 0.0;
             yield;
         }
         
         startPosition = myTransform.position;
         endPosition = gridOrientation == Orientation.Horizontal?
             Vector3(Mathf.Round(myTransform.position.x), 0.0, Mathf.Round(myTransform.position.z)) +
             Vector3(System.Math.Sign(input.x)*gridSize, 0.0, System.Math.Sign(input.y)*gridSize)
             :
             Vector3(Mathf.Round(myTransform.position.x), Mathf.Round(myTransform.position.y), 0.0) +
             Vector3(System.Math.Sign(input.x)*gridSize, System.Math.Sign(input.y)*gridSize, 0.0);
         t = tx;
         while (t < 1.0) {
             moveSpeed = Input.GetButton("Run")? runSpeed : walkSpeed;
             t += Time.deltaTime * (moveSpeed/gridSize) * (correctDiagonalSpeed  input.x != 0.0  input.y != 0.0? .7071 : 1.0);
             myTransform.position = Vector3.Lerp(startPosition, endPosition, t);
             yield;
         }
         tx = t - 1.0;   // Used to prevent slight visual hiccups on "grid lines" due to Time.deltaTime variance
         GetAxes();
     }
}

function GetAxes () {
     input = Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
     if (allowDiagonals)
         return;
     if (Mathf.Abs(input.x) > Mathf.Abs(input.y))
         input.y = 0.0;
     else
         input.x = 0.0;
}

any object with script can be activated just once(I think) what is big problem because I have whole move based on this script

I will take a look on it soon.
it seems problem is because of the lack of using update function and using
“while(true){

}”.

I will be very grateful to you if you solve this problem. This is over my ability:(

Put your movement code in your update function and disable the game object as I suggested.