Another guy who doesnt get the Destroy function to work.
So im currently chewing a bit on my problem tried various things like disabling scripts which might recreate obj and so on still doesnt work im getting a Null Reference Exception on line 22 Code:
using UnityEngine;
using System.Collections;
public class Attributes : MonoBehaviour {
public int startingHealth = 100;
void Update ()
{
if (startingHealth == 0) {
Debug.Log ("Destroy Call");
DestroyObject (gameObject);
}
}
public void Damage (int amount)
{
startingHealth = startingHealth - amount;
startingHealth = Mathf.Clamp (startingHealth, 0, 100);
Debug.Log (startingHealth);
}
}
You say the error is at line 22, but I don't think that lines up with the pasted code. You might want to indicate exactly where the error is in the above code. I assume it must be the DestroyObject call, so line 15?
Also, DestroyObject is deprecated and should be replaced with Destroy
Sry did mess smth up i tried to destroy over an other script because i dont get errors here but in the script where i tried to "remotely" destroy it`using this
using UnityEngine;
using System.Collections;
public class Attack : $$anonymous$$onoBehaviour {
private Attributes attributes;
private bool InRange = false;
void Start ()
{
attributes = new Attributes ();
}
void Update ()
{
if (InRange == true && Input.Get$$anonymous$$ouseButton (0))
attributes.Damage (34);
if (attributes.startingHealth == 0) {
DestroyObject (attributes.gameObject);
Debug.Log ("Destroy called");
}
}
void OnTriggerEnter2D (Collider2D col)
{
if (col.gameObject.CompareTag ("Zombie"))
InRange = true;
}
void OnTriggerExit2D (Collider2D col)
{
if (col.gameObject.CompareTag ("Zombie"))
InRange = false;
}
}
Answer by TBruce · Apr 30, 2016 at 10:24 PM
Your code seems to work fine. And @jgodfrey is correct about using Destroy() instead of DestroyObject(). But I just tried your code, attached it to an empty GameObject, ran the game and set startingHealth to 0 in the inspector and the GambObject was destroyed as expected with no errors.
Edit: Your problem is in the Attack class. You are trying to destroy the GameObject after it has already been destroyed.
So yeah i tried what you said and removed all connection to the code then it worked but im actually calling the attack function from an different script and game object where i copied the code from the tutorial for getting access somewhat makes it not work there here is the code i dont know why it does this error : You are trying to create a $$anonymous$$onoBehaviour using the 'new' keyword. This is not allowed. $$anonymous$$onoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all UnityEngine.$$anonymous$$onoBehaviour:.ctor()
using UnityEngine;
using System.Collections;
public class Attack : $$anonymous$$onoBehaviour {
private Attributes attributes;
private bool InRange = false;
void Start ()
{
attributes = new Attributes ();
}
void Update ()
{
if (InRange == true && Input.Get$$anonymous$$ouseButton (0)){
attributes.Damage (34);
}
}
void OnTriggerEnter2D (Collider2D col)
{
if (col.gameObject.CompareTag ("Zombie"))
InRange = true;
}
void OnTriggerExit2D (Collider2D col)
{
if (col.gameObject.CompareTag ("Zombie"))
InRange = false;
}
}
the problem is if i dont take out everything that connects the attack class to the attributes class the object doesnt get destroyed
You only need to change how you acquire a handle to the attributes class.
You can make private Attributes attributes; a public and set it in the inspector
You can do something like this (you should make sure that attributes != null before using it)
private Attributes attributes;
void Start () { attributes = this.FindObjectsOfType()[0]; }
public static T FindObjectOfType (this Object unityObject) where T : Object { return Object.FindObjectOfType(typeof(T)) as T; }
public static T[] FindObjectsOfType (this Object unityObject) where T : Object { return Object.FindObjectsOfType(typeof(T)) as T[]; }