using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class Knockback : MonoBehaviour
{
[SerializeField] private float thrust;
public float knockTime;
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("enemy"))
{
Rigidbody2D enemy = collision.GetComponent<Rigidbody2D>();
StartCoroutine(KnockCo(enemy));
}
}
private IEnumerator KnockCo(Rigidbody2D enemy)
{
if (enemy != null)
{
Vector2 forceDirection = enemy.transform.position - transform.position;
Vector2 force = forceDirection.normalized * thrust;
enemy.velocity = force;
yield return new WaitForSeconds(knockTime);
enemy.velocity = new Vector2();
}
}
}```
#I get this error when I kill the enemy MissingReferenceException: The object of type 'Rigidbody2D' h
1 messages · Page 1 of 1 (latest)
MissingReferenceException: The object of type 'Rigidbody2D' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
UnityEngine.Rigidbody2D.set_velocity (UnityEngine.Vector2 value) (at <7c971034553a4412aa7c92428f46e2e5>:0)
Knockback+<KnockCo>d__3.MoveNext () (at Assets/Script/Knockback.cs:34)
it tells u exactly what to change
It means something is trying to access Rigidbody2D on the enemy even after they've been deleted.
where are u destroying it?
using UnityEngine.Events;
public class Health : MonoBehaviour
{
public int health = 100;
public int mana = 50;
[SerializeField] private int experience = 42;
// Start is called before the first frame update
void Start()
{
Debug.Log("Current Experience " + experience); // Added so we stop getting the warning anymore
}
private int MAX_HEALTH = 100;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
if (Input.GetKeyDown(KeyCode.D))
{
health -= 20;
if (health <= 0)
{
Debug.Log("Lol, you died. Press F to pay respect!");
}
Damage(10);
}
if (health <= 0 && Input.GetKeyDown(KeyCode.F))
if (Input.GetKeyDown(KeyCode.H))
{
Heal(10);
}
}
public void Damage(int amount)
{
if (amount < 0)
{
Debug.Log("RESPECT");
throw new System.ArgumentOutOfRangeException("Cannot have negative Damage");
}
this.health -= amount;
if (health <= 0)
{
Die();
}
}
public void Heal(int amount)
{
if (amount < 0)
{
throw new System.ArgumentOutOfRangeException("Cannot have negative healing");
}
bool wouldBeOverMaxHealth = health + amount > MAX_HEALTH;
if (wouldBeOverMaxHealth)
{
this.health = MAX_HEALTH;
}
else
{
this.health += amount;
}
}
private void Die()
{
Debug.Log("I am Dead!");
Destroy(gameObject);
}
}```
!code
Posting code
📃 Large Code Blocks
Large code blocks should be posted as links to services like:
https://gdl.space/, https://paste.ofcode.org/, https://hatebin.com/
https://paste.myst.rs/, https://hastebin.com/
📃 Inline Code
Surround code with three backquotes. Not quotation marks.
To get C# formatting the first line should only contain cs or csharp.
Add a comment with a line number if there is an error message.
```cs
// Your code here
```
Do not share screenshots of code unless requested.
thats especially hard to read in a thread
i am very confused why u nested input checks, health -=20 in Update() but also a Damage function right below it
but regardless, the issue comes because something is entering the trigger after u die
u will need to organize how you do this. Does the enemy take damage from something entering its collider? When does the enemy get knocked back
Yes, u are killing the enemy before it is knocked back or while
I'm thinknig that maybe you hit enemy, deal damage, msot of knockback logic runs.
Then because of this:
yield return new WaitForSeconds(knockTime);
enemy.velocity = new Vector2();
Maybe Destroy happens before that last velocity thing does.
u also have a "knockTime" which i assume means your enemy is knocked back for a few frames
I tried doing separate shots Its still the same
yes
so if its a few frames, and they die in update which is every frame...
you must reorganize this for it to work. Destroying should only be called after its done being used. U can still have it "die" and just disable it from moving until it is destroyed
it should not be instantly destroyed on death especially if you want it to be knockedback during this
I was typing basically the same thing
You can either add a null check to that last velocity thing, or you can make the die function just disable the gameObject and destroy it with a delay.
null check is hacky
U will need a null check on every single line that references the object
also destroying it with a delay is somewhat hacky. The enemy should have a duration for knockback THEN be destroyed, unless it can be destroyed in the middle of that
Oh thanks it worked
but I get an error
still can play
NullReferenceException: Object reference not set to an instance of an object
UnityEditor.Graphs.Edge.WakeUp () (at <4fa92967e90043cbb25cde5b275e9e4e>:0)
UnityEditor.Graphs.Graph.DoWakeUpEdges (System.Collections.Generic.List1[T] inEdges, System.Collections.Generic.List1[T] ok, System.Collections.Generic.List`1[T] error, System.Boolean inEdgesUsedToBeValid) (at <4fa92967e90043cbb25cde5b275e9e4e>:0)
UnityEditor.Graphs.Graph.WakeUpEdges (System.Boolean clearSlotEdges) (at <4fa92967e90043cbb25cde5b275e9e4e>:0)
UnityEditor.Graphs.Graph.WakeUp (System.Boolean force) (at <4fa92967e90043cbb25cde5b275e9e4e>:0)
UnityEditor.Graphs.Graph.WakeUp () (at <4fa92967e90043cbb25cde5b275e9e4e>:0)
UnityEditor.Graphs.Graph.OnEnable () (at <4fa92967e90043cbb25cde5b275e9e4e>:0)
dont know where the error is
Fair enough. I usually have some nullcheck that encompasses all damage sending but with this structure it'd be really messy to do.
Also crossed my mind that if anything needs a longer lasting reference than the destroy()'s delay then it would break.
Uh.
In this case the null reference means that the code wants to change something but there's no reference to it.
That's a unity editor error . . .
okay thanks found it