- Home /
Work around for, "Getting control 0's position in a group with only 0 controls when doing repaint?" (Trouble Spot Spotted)
So, I left for dinner after I concluded that an error was caused by Unity being drunk (bug in unity,) and then I come back, and I'm satisfied that I am right! Until I hit the play button...
EDIT! I have found out the trouble spot for the error, and it's at the fittingWindow(int id) function. What happens is that if I put a GUI element in this function, the error is spit out. How does one work around this issue?
There are 2 scripts that work together, 1 in a gameobject and a few others as children of that gameobject. (I don't think this matters as it seems a OnGUI() issue rather than a script communication issue.
-This is the fitting script. (As parent)
public class fittingScript : Photon.MonoBehaviour {
//Remember to change the unfit id to 20 if exceeding 10 turrets
public bool isEditor = true;
private Vector2 scrollPosition = new Vector2(300,300);
private Rect fittingServiceRect = new Rect(10,10,Screen.width / 5 - 20, Screen.height/5 - 20);
private Rect subWindowRect = new Rect(10,Screen.height / 5 * 3 + 10,Screen.width / 5 * 2 - 20, Screen.height/5 * 2 - 20);
private Rect statsRect = new Rect(Screen.width/5 * 4 - 20, 10, Screen.width/5 - 20, Screen.height/5 * 2 - 20);
public hardpointInitialization[] hardpoints; //How we find our hardpoints. Hardpoints must contain this to be recognized.
private hardpointInitialization selectedPoint; //The currently selected point.
private int currentlySelectedItem = 0;
//Ship Stats For Saving and Fitting
public string ship = "";
public float power = 0.0f;
public float maxPower = 100.0f;
public float detection = 100.0f;
public float speed = 0.0f;
public float maxHp = 0.0f;
// Use this for initialization
void Start () {
hardpoints = new hardpointInitialization[GetComponentsInChildren<hardpointInitialization>().Length];
hardpoints = GetComponentsInChildren<hardpointInitialization>();
foreach (hardpointInitialization h in hardpoints) {
}
Debug.Log(hardpoints);
if (isEditor) {
return;
}
//Do all the turret instantiation and locking here.
}
void Update () {
foreach(hardpointInitialization hp in hardpoints) {
power += hp.powerRequirement;
detection += hp.stealthPenalty;
speed -= hp.speedPenalty;
}//
}
// Update is called once per frame
void OnGUI () {
if (isEditor) {
GUI.Window(0,fittingServiceRect,fittingEditor,"Fitting");
GUI.Window(2,statsRect,shipInfoWindow,"Ship Stats");
if (selectedPoint != null) {
GUI.Window(1,subWindowRect,selectedGunWindow,"Selected Slot");
}
}
}
//BELOW IS EDITOR FUNCTIONS
//--------------------------------------------------------------------------------------------------
void fittingEditor (int id) {
foreach(hardpointInitialization hp in hardpoints) {
GUILayout.BeginHorizontal();
if(GUILayout.Button("|" + hp.itemName + "|")) { //IT HAPPENS RIGHT HERE.
selectedPoint = hp; //REMOVE THE IF STATEMENT WITH THE GUI ELEMENT AND THE ERROR GOES AWAY. HOW DO I WORK AROUND THIS?
}
GUILayout.EndHorizontal();
}
}
void selectedGunWindow(int id) {
//The item database is stored here!
if (selectedPoint == null) {
return;
}
GUILayout.BeginHorizontal();
if (GUILayout.Button("<")) {
currentlySelectedItem--;
}
switch(currentlySelectedItem) {
case 0:
GUILayout.Label("X-4 Heavy Railgun Double");
break;
case 1:
GUILayout.Label("X-2 Saron Laser Double");
break;
case 2:
GUILayout.Label("L-32 Flyswatter");
break;
}
if (GUILayout.Button(">")) { //Make the item selection go up
currentlySelectedItem++;
}
GUILayout.EndHorizontal();
scrollPosition = GUILayout.BeginScrollView(scrollPosition,GUILayout.Width (Screen.width/5*2),GUILayout.Height(100));
switch(currentlySelectedItem) {
case 0:
GUILayout.TextArea("This is a heavy railgun double. Nothin special... YET!",GUILayout.Width(Screen.width/5 * 2-40));
break;
case 1:
GUILayout.TextArea(" Stuff about this gun",GUILayout.Width(Screen.width/5 * 2-40));
break;
case 2:
GUILayout.TextArea(" Stuff about this gun",GUILayout.Width(Screen.width/5 * 2-40));
break;
}
GUILayout.EndScrollView();
currentlySelectedItem = Mathf.Clamp(currentlySelectedItem,0,2);
if(GUILayout.Button("Fit")) {
//TODO add checks for power, calibration etc.
switch(currentlySelectedItem) {
case 0:
selectedPoint.itemName = "turret_HeavyRailgunDouble";
selectedPoint.itemId = 0;
//ITEM STATISTICS!
//--------------------------------
selectedPoint.editorChange ();
//TODO edit power and stuff.
break;
case 1:
selectedPoint.itemName = "turret_SaronDouble";
selectedPoint.itemId = 1;
//ITEM STATISTICS!
//--------------------------------
selectedPoint.editorChange ();
break;
case 2:
selectedPoint.itemName = "turret_FlySwatter";
selectedPoint.itemId = 2;
//ITEM STATISTICS!
//--------------------------------
selectedPoint.editorChange ();
break;
}
}
}
void shipInfoWindow (int id) {
GUILayout.Label("Ship: " + ship);
GUILayout.Label("_______________");
if (power > maxPower) {
GUI.contentColor = Color.red;
GUILayout.Label("Power: (" + power.ToString() + " GW/" + maxPower.ToString() + " GW)");
GUI.contentColor = Color.white;
}
else {
GUILayout.Label("Power: (" + power.ToString() + " GW/" + maxPower.ToString() + " GW)");
}
GUILayout.Label("Detection :" + detection.ToString() + " U");
GUILayout.Label("Speed: " + speed.ToString() + " U/second");
}
}
This is the hardpointInitialization script. (As child gameObjects.) In this case I tested with three child gameobjects with this script attached to them.
public class hardpointInitialization : Photon.MonoBehaviour {
//This class should only be used for turret initialization and storage of data.
public int hardpointType = 0; //0 = turret, 1 = Utility, 2 = Capital.
public string itemName = "None"; //This is used for identifying the item the hardpoint has. Will be updated via script.
public int itemId = 0; //This is the more explicit way of doing things.
public bool isEnabled = false;
public GameObject currentTurret = null;
public GameObject currentTurretInEditor = null;
public fittingScript editor;
public GameObject[] turretModels;
public float powerRequirement = 0.0f;
public float speedPenalty = 0.0f;
public float stealthPenalty = 0.0f;
void Start () {
if (!isEnabled) {
currentTurret = PhotonNetwork.Instantiate(itemName,transform.position,transform.rotation,0);//What this does is that it creates the turret TODO Test it.
currentTurret.transform.parent = gameObject.transform; //Then locks it in place. This is all according to the fitting menu TODO make it according to fitting menu.
}
}
void Update () {
}
public void editorChange () {
if (!isEnabled) {
return;
}
if(currentTurretInEditor != null) {
GameObject.Destroy(currentTurretInEditor);
powerRequirement = 0f;
speedPenalty = 0f;
stealthPenalty = 0f;
}
switch (itemId) {
case 0:
currentTurretInEditor = GameObject.Instantiate(turretModels[0],transform.position,transform.rotation) as GameObject; //Heavy Double Railgun
currentTurretInEditor.transform.parent = gameObject.transform;
powerRequirement = 30f;
speedPenalty = 1f;
break;
case 1:
currentTurretInEditor = GameObject.Instantiate(turretModels[1],transform.position,transform.rotation) as GameObject; //Saron Double
currentTurretInEditor.transform.parent = gameObject.transform;
powerRequirement = 25f;
speedPenalty = 1f;
break;
case 2:
currentTurretInEditor = GameObject.Instantiate(turretModels[2],transform.position,transform.rotation) as GameObject; //Flyswatter
currentTurretInEditor.transform.parent = gameObject.transform;
powerRequirement = 20f;
speedPenalty = 0f;
break;
case 10:
Debug.Log("Nothing!");
break;
}
}
}