Having problem with Destroying GameObject

Updated on October 12, 2018 in Answers
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
10 on October 3, 2018

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 Script

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;
}
}
}
}
  • Liked by
Reply
9 on October 3, 2018

You destroy the ‘Floor’ GameObject itself, but still trying to use it in the LateUpdate method. I think what you want to destroy is not the floor, but the GameObject you instantiate in the ‘spawnFloors’ method. So you have to save that instantiated GameObject globally and destroy this then later when it is beneath the camera view.

 

on October 4, 2018

Please help me with the code I’m still learning and that’s why I did not understand what you just said, sorry though.. I think it’s already global(public void spawnFloors()) if that’s what you mean by

Helpful
on October 4, 2018

Okay then I have probably a better solution for you. Look at this Unity tutorial here:

Unity Tutorial Space Shooter

Build in your game also a boundary which destroys every platform which leaves the boundary box. So you can remove your own ‘DestroyObject()’ method completly, you don’t need it anymore and with that the problem should be gone.

 

on October 8, 2018

I wrote this script Boundry.cs

public class Boundry : MonoBehaviour
{
     public GameObject floor;
     void OnTriggerExit2D()
     {
          if (floor != null)
          {
               Destroy(floor);
          }
     }
}

first two platform is destroying than that error comes up

Helpful
on October 8, 2018

You don’t need to create a floor GameObject there.

Just this code is enough, if your player can’t reach the boundary:

void OnTriggerExit2D(Collider2D other)
{
Destroy(other.gameObject);
}
on October 9, 2018

Man, you’re a god for me, thank you so much for helping,

and one more thing as you can see Repeat Script above I used maxY++

so what it does is, every single time my camera goes up it generates double amount of platforms like first jump 2 generated, second jump 4 and third jump 8….so is there anyway to control it, I want it to generate only 4 or 5 every single time I jump

BTW really appreciate on Destroying thing though

Helpful
on October 9, 2018

You can use

maxY = Mathf.Clamp(maxY, 1, 5);

to clamp the value of maxY between two numbers (here 1 and 5), if it is what you want.

Moreover I saw that your for loop is not neccessary here:

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);
}
}

It will always break out after the first walkthrough. To use it more correctly you would have to change i<=0 to something else. But I don’t think that you need the for loop at all though.

on October 10, 2018

can you suggest me something else what can I use instead for loop(in code)

I’m still noob in these things,

I think function is calling as I go up, I saw in the console there’s already 40 or 50 floors as I jump

and as soon as I jump again it is just doubling the floors

I’ve uploaded my project to my drive…take a look if you have some free time

https://drive.google.com/open?id=1t-oPRrIKK6ikOpZFpG9LT9NtJTaNBWYs

Helpful
on October 10, 2018

I think the problem is that you spawn in the ‘LateUpdate()’ method every frame a new platform if your target is in a higher position than your camera.

 

I don’t know how your camera is moving, but I think there a multiple frames in which your target is in a higher y position than your camera. That means every frame a platform will be spawned. That’s why you have so much platforms there.

 

So you can create a boolean like ‘isHigherThanCamera’ and do something like that:

 

void LateUpdate ()
{
if (target.position.y < transform.position.y)
{
isHigherThanCamera = false;
}
if (target.position.y > transform.position.y && !isHigherThanCamera)
{
isHigherThanCamera = true;
Vector3 newPos = new Vector3(transform.position.x, target.position.y, transform.position.z);
transform.position = newPos;
floor.GetComponent<RepeatLevel>().spawnFloors();
}
}
on October 12, 2018

Still no luck 🙁

Show more replies
  • Liked by
Reply
Cancel