Question by
UnityNoob123 · Nov 18, 2016 at 09:24 PM ·
gameobject
Have a gameobject switch its values?
Thanks for checking out my question. I am have a game where you play as ships, only one ship is active in the hierarchy at a time and each ship is controlled by the same left and right button. The control script I have only works for the ship that is active in the hierarchy on the initial startup. When I switch ships, which my control script accounts for, it does not allow the buttons to move that ship like how it did with the first ship. I am trying to have a gameobject in my control script to be set equal to the ship that is active in the hierarchy at the time being and use that in my button functions. Any help is appreciated, thanks!
using UnityEngine;
using System.Collections;
public class buttons : MonoBehaviour {
GameObject go;
float speed;
bool goRightBool = false;
bool goLeftBool = false;
public GameObject ship1;
public GameObject ship2;
public GameObject ship3;
public GameObject ship4;
public GameObject ship5;
public GameObject ship6;
public GameObject ship7;
public GameObject ship8;
public GameObject ship9;
// Use this for initialization
void Start () {
go = null;
}
// Update is called once per frame
void Update () {
if (shipShop.shipSelected == 1) {
go = null;
go = ship1;
speed = playerController.playerSpeed;
}
if (shipShop.shipSelected == 2) {
go = null;
go = ship2;
speed = playerController.playerSpeed;
}
if (shipShop.shipSelected == 3) {
go = ship3;
speed = playerController.playerSpeed;
}
if (shipShop.shipSelected == 4) {
go = null;
go = ship4;
Debug.Log ("ship4GO");
speed = playerController2.playerSpeed;
}
if (shipShop.shipSelected == 5) {
go = null;
go = ship5;
Debug.Log ("ship5GO");
speed = playerController2.playerSpeed;
}
if (shipShop.shipSelected == 6) {
go = ship6;
Debug.Log ("ship6GO");
speed = playerController2.playerSpeed;
}
if (shipShop.shipSelected == 7) {
go = null;
go = ship7;
speed = playerController3.playerSpeed;
}
if (shipShop.shipSelected == 8) {
go = null;
go = ship8;
speed = playerController3.playerSpeed;
}
if (shipShop.shipSelected == 9) {
go = null;
go = ship9;
speed = playerController3.playerSpeed;
}
if (goRightBool == true) {
go.transform.Translate (Vector2.right * speed);
playerController.rotInt = 1;
playerController2.rotInt2 = 1;
playerController3.rotInt3 = 1;
Debug.Log ("lean right");
} else if (goLeftBool == true) {
go.transform.Translate (Vector2.left * speed);
playerController.rotInt = 2;
playerController2.rotInt2 = 2;
playerController3.rotInt3 = 2;
Debug.Log ("lean right");
} else {
playerController.rotInt = 0;
playerController2.rotInt2 = 0;
playerController3.rotInt3 = 0;
}
}
public void goRight() {
goRightBool = true;
}
public void goRightNo() {
goRightBool = false;
}
public void goLeft() {
goLeftBool = true;
}
public void goLeftNo() {
goLeftBool = false;
}
}
Comment