- Home /
Question by
dhvl5 · Oct 03, 2018 at 11:39 AM ·
gameobjectinstantiatedestroyrepeatfloor
Having problem with Destroying GameObject
ERROR - the object of type gameobject has been destroyed but you are still trying to access it
So I'm creating Doodle Jump for learning and I want as soon as my camera and player goes up The Floor/Platform should be Destoyed....And here's my scripts
CameraScript
using UnityEngine;
public class CameraScript : MonoBehaviour {
public Transform target; public GameObject floor; public GameObject platform; void LateUpdate () { if (target.position.y > transform.position.y) { Vector3 newPos = new Vector3(transform.position.x, target.position.y, transform.position.z); transform.position = newPos; floor.GetComponent<RepeatLevel>().spawnFloors(); DestroyObject(); } } public void DestroyObject() { if (target.position.y > 10f) { Destroy(this.floor); } } }
RepeatLevel Script
using UnityEngine;
public class RepeatLevel : MonoBehaviour {
public GameObject platformPrefab; private float minY = 0f; private float maxY = 1f; private float levelWidth = 3f; public void spawnFloors() { Vector3 spawnPosition = new Vector3(); for (int i=0; i<=0; i++) { spawnPosition.y = maxY++; spawnPosition.x = Random.Range(-levelWidth, levelWidth); Instantiate(platformPrefab, spawnPosition, Quaternion.identity); } } }
Floors Script
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Floors : MonoBehaviour {
float jump = 10f; void OnCollisionEnter2D(Collision2D collision) { if (collision.relativeVelocity.y <= 0) { Rigidbody2D rb2d = collision.collider.GetComponent<Rigidbody2D>(); if (rb2d != null) { Vector2 velocity = rb2d.velocity; velocity.y = jump; rb2d.velocity = velocity; } } } }
Comment