Quantcast
Channel: Questions in topic: "error message"
Viewing all articles
Browse latest Browse all 2891

The variable checkpointObjects of player has not been assigned.

$
0
0
I am trying to make a game similar to Geometry Dash, and I started to create the practice mode system. I tried adding flags to indicate that you placed a checkpoint, but I can't manage to make the flags disappear once the checkpoint is deleted. I tried using an array of GameObjects, but it just gives me "The variable checkpointObjects of player has not been assigned." when I remove the checkpoint. Here's the code (everything works fine in this, I only get the checkpointObjects error): public class player : MonoBehaviour { public bool grounded; public float jumpHeight; public Vector3 respawnPos; public bool isPracticing; public Vector3[] checkpoints = new Vector3[999]; public Vector3[] checkpointsPlayer = new Vector3[999]; public GameObject[] checkpointObjects = new GameObject[999]; public int cIndex; private bool dead; private GameObject result; public void Die() { dead = true; transform.position = new Vector3(0,-5000,0); GameObject.Find("level").GetComponent().bSpeed = 0; StartCoroutine(Respawn()); } public GameObject FindObject(string name) { GameObject result = new GameObject(); foreach(GameObject t in Resources.FindObjectsOfTypeAll()) { if (t.name == name) { result = t; } } return result; } public IEnumerator Respawn() { yield return new WaitForSeconds(2); transform.position = respawnPos; dead = false; foreach (GameObject gaming in Resources.FindObjectsOfTypeAll()) { if (gaming.GetComponentInParent()) { gaming.SetActive(true); } } GameObject.Find("level").GetComponent().transform.position = GameObject.Find("level").GetComponent().defaultPos; GameObject.Find("level").GetComponent().bSpeed = GameObject.Find("level").GetComponent().speedIWant; } public GameObject GetLatestCheckpoint() { foreach (GameObject gameObj in Resources.FindObjectsOfTypeAll()) { if (gameObj.name.StartsWith("pFlag") && gameObj.active != false) { if (gameObj.transform.position == checkpointsPlayer[cIndex]) { result = gameObj; } } } return result; } public void Checkpoint() { GameObject p = Instantiate(Resources.Load("prefab/pFlag")); p.transform.SetParent(GameObject.Find("level").transform); p.transform.position = transform.position + new Vector3(0, 0.3f, 0); checkpointObjects[cIndex] = p; checkpoints[cIndex] = GameObject.Find("level").transform.position; GameObject.Find("level").GetComponent().defaultPos = checkpoints[cIndex]; checkpointsPlayer[cIndex] = transform.position + new Vector3(0, 0.3f, 0); respawnPos = checkpointsPlayer[cIndex]; cIndex++; } public void RemCheckpoint() { if (cIndex != 0) { checkpoints[cIndex] = Vector3.zero; checkpointsPlayer[cIndex] = Vector3.zero; checkpointObjects[cIndex].SetActive(false); cIndex--; GameObject.Find("level").GetComponent().defaultPos = checkpoints[cIndex]; respawnPos = checkpointsPlayer[cIndex]; } else { checkpoints[0] = Vector3.zero; checkpointsPlayer[0] = Vector3.zero; checkpointObjects[0].SetActive(false); GameObject.Find("level").GetComponent().defaultPos = checkpoints[0]; respawnPos = checkpointsPlayer[0]; } } void OnCollisionEnter2D(Collision2D other) { if (other.gameObject.CompareTag("Spike")) { Die(); } else if (other.gameObject.CompareTag("CubeKill")) { Die(); } else if (other.gameObject.CompareTag("GroundObject")) { grounded = true; } } void OnCollisionExit2D(Collision2D other) { if (other.gameObject.CompareTag("GroundObject")) { grounded = false; } else { grounded = false; } } void Jump() { if (grounded) { GetComponent().velocity = new Vector2(0, jumpHeight); GetComponentInChildren().Play("jump"); } } void Start() { cIndex = 0; } void Update() { // ANTI-BUG PROTOCOL? \\ if (transform.position.x == 0 && transform.position.y < -499 && dead != true) { transform.position = new Vector3(-7.68f, -2.55f, 0); // 0.02230277 // -0.485862 // -12.95996 checkpoints[cIndex] = new Vector3(0.02230277f, -0.485862f, -12.95996f); GameObject.Find("level").GetComponent().defaultPos = new Vector3(0.02230277f, -0.485862f, -12.95996f); } // ANTI-BUG PROTOCOL? \\ if (Input.GetKeyDown(KeyCode.Z)) { if (isPracticing) { Checkpoint(); } else { FindObject("pModeText").active = true; isPracticing = true; Checkpoint(); } } if (Input.GetKeyDown(KeyCode.X)) { if (isPracticing && cIndex != 0) { RemCheckpoint(); } } if (Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0)) { Jump(); } } } I have searched this error and found no answer (found some that said there was another gameobject with the script, but there's no gameobject with the script, and clicking the error brings me to the player gameobject, which has only one of the script as component). Please help!

Viewing all articles
Browse latest Browse all 2891

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>