Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Digger2011 · Apr 30, 2016 at 10:12 PM · gameobjectdestroyworkdont

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jgodfrey · Apr 30, 2016 at 10:19 PM 0
Share

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

avatar image Digger2011 · Apr 30, 2016 at 10:24 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by TBruce · Apr 30, 2016 at 10:24 PM

@Digger2011

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Digger2011 · Apr 30, 2016 at 10:41 PM 0
Share

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

avatar image Digger2011 · Apr 30, 2016 at 10:55 PM 0
Share

the problem is if i dont take out everything that connects the attack class to the attributes class the object doesnt get destroyed

avatar image TBruce Digger2011 · Apr 30, 2016 at 11:32 PM 0
Share

@Digger2011

You only need to change how you acquire a handle to the attributes class.

  1. You can make private Attributes attributes; a public and set it in the inspector

  2. 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[]; }

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

65 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroyed instance of Prefab, can't spawn it back. 1 Answer

I can't destroy my instantiated game object 1 Answer

Destroy(GameObject) works after 3 attempts 0 Answers

No error code? Not sure where to go? 1 Answer

Collision detection in 2017 3d 4.2f2 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges