Loading Game Crash
Hey guys! Creeperbot65 here!
I'm having a problem with loading data and then creating instances with that data. It's kinda hard to explain but here are the scripts (All written in C#)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InstanceCreating : MonoBehaviour {
public float PlacementTime;
private double Wait;
public Dropdown MeshDropdown;
public Slider SizeSlider;
public GameObject CubePrefab;
public GameObject SpherePrefab;
public GameObject CylinderPrefab;
public GameObject CapsulePrefab;
public Transform PositionSpawn;
public Camera Cam;
public Quaternion Rotation;
public float InstantiateDistance;
public int OBJsCreated;
//Loading Vars
public Button LoadButton;
public bool LoadAll = false;
private int Instancestoload = 0;
private int Mesh;
private float X;
private float Y;
private float Z;
public bool KillIns;
void Start () {
Button btn = LoadButton.GetComponent<Button>();
btn.onClick.AddListener(TaskOnClick);
}
void Awake () {
OBJsCreated = 1;
}
void FixedUpdate () {
InstantiateDistance += Input.GetAxis("Mouse ScrollWheel");
if (Input.GetKey(KeyCode.E) && Time.time > Wait) {
Vector3 temp = Cam.transform.position;
if (MeshDropdown.value == 0) {
Instantiate (CubePrefab, Cam.transform.position + Cam.transform.forward * InstantiateDistance , Rotation);
} else if (MeshDropdown.value == 1) {
Instantiate (SpherePrefab, Cam.transform.position + Cam.transform.forward * InstantiateDistance , Rotation);
} else if (MeshDropdown.value == 2) {
Instantiate (CapsulePrefab, Cam.transform.position + Cam.transform.forward * InstantiateDistance , Rotation);
} else if (MeshDropdown.value == 3) {
Instantiate (CylinderPrefab, Cam.transform.position + Cam.transform.forward * InstantiateDistance , Rotation);
}
OBJsCreated = OBJsCreated + 1;
Wait = Time.time + PlacementTime;
}
}
void TaskOnClick () {
LoadAll = true;
Instancestoload = PlayerPrefs.GetInt ("InsCreated");
Wait = Time.time + 0.2;
while (Time.time < Wait) {
KillIns = true;
}
KillIns = false;
OBJsCreated = 1;
for (int OBJsCreated = 1; OBJsCreated > Instancestoload; OBJsCreated++) {
Mesh = PlayerPrefs.GetInt (OBJsCreated + "_Mesh");
X = PlayerPrefs.GetFloat (OBJsCreated + "_X");
Y = PlayerPrefs.GetFloat (OBJsCreated + "_Y");
Z = PlayerPrefs.GetFloat (OBJsCreated + "_Z");
if (Mesh == 0) {
Instantiate (CubePrefab, new Vector3 (X, Y, Z), Rotation);
} else if (Mesh == 1) {
Instantiate (SpherePrefab, new Vector3 (X, Y, Z), Rotation);
} else if (Mesh == 2) {
Instantiate (CylinderPrefab, new Vector3 (X, Y, Z), Rotation);
} else if (Mesh == 3) {
Instantiate (CapsulePrefab, new Vector3 (X, Y, Z), Rotation);
}
}
LoadAll = false;
}
}
That was script one and here's the other for instances that have been created.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class InsScript : MonoBehaviour {
public Renderer rend;
public Texture GrassTexture;
public Dropdown MaterialDD;
public Slider SizeSlider;
public Toggle PhysicsTogg;
public Camera MainCam;
private int InsNo;
public int Material;
public float Scale;
public bool Save = false;
public Dropdown MeshDD;
public int Mesh;
private bool LoadAll = false;
private bool KillINS = false;
// Use this for initialization
void Awake () {
//Gets material dropdown from StuffForCubePrefab script
GameObject GameController = GameObject.Find("GameController");
StuffForCubePrefab MaterialPrefabScript = GameController.GetComponent<StuffForCubePrefab>();
InstanceCreating InstCreateScript = GameController.GetComponent<InstanceCreating>();
InsNo = InstCreateScript.OBJsCreated;
MeshDD = MaterialPrefabScript.MeshDD;
MaterialDD = MaterialPrefabScript.MaterialDD;
GrassTexture = MaterialPrefabScript.GrassTexture;
SizeSlider = MaterialPrefabScript.SizeSlider;
PhysicsTogg = MaterialPrefabScript.PhysicsTogg;
MainCam = MaterialPrefabScript.MainCam;
PhysicsTogg = MaterialPrefabScript.PhysicsTogg;
Mesh = MeshDD.value;
InstanceCreating LoadScript = GameController.GetComponent<InstanceCreating>();
LoadAll = LoadScript.LoadAll;
if (LoadAll == false) {
if (MaterialDD.value == 0) {
rend.material.mainTexture = GrassTexture;
}
Material = MaterialDD.value;
transform.localScale = new Vector3 (SizeSlider.value, SizeSlider.value, SizeSlider.value);
Scale = SizeSlider.value;
} else {
Material = PlayerPrefs.GetInt (InsNo + "_Mat");
Scale = PlayerPrefs.GetFloat (InsNo + "_Size");
//sets Material
if (Material == 0) {
rend.material.mainTexture = GrassTexture;
}
//sets Size
transform.localScale = new Vector3 (Scale, Scale, Scale);
}
}
// Update is called once per frame
void FixedUpdate () {
if (PhysicsTogg.isOn == false) {
transform.rotation = Quaternion.Euler(0, 0, 0);
Rigidbody RigidBody = GetComponent<Rigidbody>();
RigidBody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezePositionX;
} else {
Rigidbody RigidBody = GetComponent<Rigidbody>();
RigidBody.constraints = RigidbodyConstraints.None;
}
GameObject GameController = GameObject.Find("GameController");
SaveAll SaveScript = GameController.GetComponent<SaveAll>();
Save = SaveScript.Save;
if (Save == true) {
PlayerPrefs.SetInt (InsNo + "_Mesh", Mesh);
PlayerPrefs.SetInt (InsNo + "_Mat", Material);
PlayerPrefs.SetFloat (InsNo + "_Size", Scale);
PlayerPrefs.SetFloat (InsNo + "_X", transform.position.x);
PlayerPrefs.SetFloat (InsNo + "_Y", transform.position.y);
PlayerPrefs.SetFloat (InsNo + "_Z", transform.position.z);
}
InstanceCreating InsCreScript = GameController.GetComponent<InstanceCreating>();
KillINS = InsCreScript.KillIns;
if (KillINS == true) {
Destroy (this.gameObject);
}
}
}
Thanks for helping!
-creeperbot65
Could you please post the errors that appear in the console when the game crashes?
Answer by AdmiralThrawn · Dec 19, 2017 at 12:05 PM
Hard to help you without knowing the exact error. So if you like to receive some help, make sure to provide all necessary information, that is at least the copy of the error stack trace shown in the Unity log window.
You frequently use code like:
Save = SaveScript.Save; if (Save == true) { ... }
a) If you don't use Save anywhere else, there is no reason to store SaveScript.Save into a global or local variable. Therefor you can simply use
if (SaveScript.Save) { ... }
b) You don't need to compare bool variables via '==' for comparison.
if (isSomethingTrue) { ...}
or the opposite case
if (!isSomethingFalse) { /* run only if isSomethingFalse is false */ ... }
will use a minimum of code and increase the readability (given the case you actually use proper naming for variables, methods, etc.
On a side note: The book 'Clean Code' by Robert C. Martin is a good read for any programmer. :)
Ok, Thanks! But nothing appears in the console, it crashes unity. I'll see if it works in the new version of Unity.