Reputation: 11
I'm programming a game when the player dashes into a box it should break isntantiate broken pieces sprites which should disappear after a certain amount of time.
using System.Collections;
using System.Collections.Generic; using UnityEngine;
public class BrokenPieces : MonoBehaviour { // Start is called before the first frame update
public float moveSpeed = 3f;
private Vector3 moveDirection;
public float deceleration = 5f;
public float lifetime = 3f;
public SpriteRenderer theSR;
public float fadeSpeed = 2.5f;
void Start()
{
moveDirection.x = Random.Range(-moveSpeed, moveSpeed);
moveDirection.y = Random.Range(-moveSpeed, moveSpeed);
}
// Update is called once per frame
async void Update()
{
transform.position += moveDirection * Time.deltaTime;
moveDirection = Lerp(moveDirection, Vector3.zero, deceleration * Time.deltaTime);
lifetime -= Time.deltaTime;
if(lifetime < 0)
{
theSR.color=new Color(theSR.color.r, theSR.color.g, theSR.color.b, Mathf.MoveTowards(theSR.color.a, 0f, fadeSpeed * Time.deltaTime));
if(theSR.color.a == 0f)
{
Destroy(gameObject);
}
}
}
}
using System.Collections;
using System.Collections.Generic; using UnityEngine;
public class Breakables : MonoBehaviour { public GameObject[] brokenPieces;
public int maxPieces = 5;
public bool shouldDropItem;
public GameObject[] itemsToDrop;
public float itemDropPercent;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Player")
{
if(PlayerController.instance.dashCounter > 0)
{
Destroy(gameObject);
// show broken pieces
int piecesToDrop = Random.Range(1, maxPieces);
for(int i = 0; i < piecesToDrop; i++)
{
int randomPiece= Random.Range(0, brokenPieces.Length);
Instantiate(brokenPieces[randomPiece], transform.position, transform.rotation);
}
// drop items
if(shouldDropItem)
{
float dropChance = Random.Range(0f,100f);
if(dropChance < itemDropPercent)
{
int randomItem = Random.Range(0, itemsToDrop.Length);
Instantiate(itemsToDrop[randomItem], transform.position, transform.rotation);
}
}
}
}
}
}
Upvotes: 1
Views: 154
Reputation: 90580
So your actual error has nothing todo with the timing.
The object of type 'GameObject' has been destroyed but you are still trying to access it. this error comes up when i break the box
This happens because you destroy your object
Destroy(gameObject);
and then after it is already destroyed try to access it's transform.position
.
Simply either cache the position and rotation beforehand
var position = transform.position;
var rotation = transform.rotation;
Destroy(gameObject);
// show broken pieces
int piecesToDrop = Random.Range(0, maxPieces) + 1;
for(int i = 0; i < piecesToDrop; i++)
{
int randomPiece= Random.Range(0, brokenPieces.Length);
Instantiate(brokenPieces[randomPiece], position, rotation);
}
or make sure to put the
Destroy (gameObject);
at the bottom of your method so it isn't trying to access any of it's properties after destroying it.
Upvotes: 2