• 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 /
avatar image
0
Question by CodeTurtle · Feb 16, 2014 at 08:24 AM · script error

Destroying all clones using other scripts

Hi, I'm new here. There's a Unity problem that's been bugging me for a few days.

I'm trying to make a game interacting with some characters forming a circle. If I click a button, these characters moves away and gets deleted. So that other characters can get in the circle.

However, every time I try to delete these clones, an error appears. Looking like this: "NullReferenceException: Object reference not set to an instance of an object"

I've tried using the gameObject.Find command, but this only allows me to delete one clone when the button is clicked.

Here's my code:

 //Instantiate.cs
 public Rigidbody2D alienPrefab ;
     public float Number ;
     public Rigidbody alienInstance;
     public DestroyThem other ;
 
     void Start () {
 
         MakePeople ();
     }
 
     void Update () {
 
     }
 
     void OnMouseDown () {
         other.DestroyPeople ();
         //MakePeople ();
     }
 
     void MakePeople () {
         float Degrees ;
         int i ;
 
         Number = Random.Range (2, 10);
         Degrees = (2*Mathf.PI)/Number ;
         
         for ( i = 1 ; i < Number ; i++ ) {
             alienInstance = Instantiate( alienPrefab , new Vector3(5*Mathf.Cos (Degrees*i) ,5*Mathf.Sin (Degrees*i),0) , transform.rotation ) as Rigidbody ;
         }
     }

 
 //DestroyThem.cs
 public Instantiate ins ;
 
     public void DestroyPeople() {
         //if ( ins.alienInstance != null)
         //Destroy (gameObject);
         //Destroy (ins.alienInstance);
         //Destroy (GameObject.Find("OtherCharacters2(Clone)").rigidbody );
         Destroy (GameObject.Find ("OtherCharacters2(Clone)")); //<---Here's the problem!
         //Destroy (GameObject.FindGameObjectsWithTag("Others"));
         //DestroyFlag = true;
 
     }
Comment
Add comment · Show 5
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 robertbu · Feb 16, 2014 at 08:26 AM 0
Share

Please editor your question and add the text of the error message from the console and/or mark the line in the code that is generating the error.

avatar image CodeTurtle · Feb 16, 2014 at 09:20 AM 0
Share

O$$anonymous$$, I marked the error message.Also pointed out the problem where I think it is at.

avatar image CodeTurtle · Feb 16, 2014 at 09:23 AM 0
Share

The function: Destroy(GameObject.Find("OtherCharacters2(Clone)")); only destroys one clone at a time. However, I wanted all clones who has the same name to be deleted.

avatar image Qasem2014 · Dec 16, 2014 at 06:11 PM 0
Share

on << void On$$anonymous$$ouseDown() >> function , you use this :

 other.DestroyPeople ();

whats the "other" ?

avatar image CodeTurtle · Dec 16, 2014 at 08:01 PM 0
Share

"other" is the variable for the code "DestroyThem", which is the second code I posted.

3 Replies

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

Answer by CodeTurtle · Dec 16, 2014 at 05:41 PM

It has been 10 months since I've touched Unity. Looking back at this post, I suppose I should reply an answer to this old amateur me.

GameObject.Find() can only find one object at a time, which is the first object counting down from the top of hierarchy.

If you want to destroy all the gameobjects with one call, for instance KeyCode.E. You will have to attach a "destroy code" to the prefab before you want to instantiate it. Then call the "destroy code" with another code.

 //Destroy.cs 
 //Put this in your prefab before instantiating it
     
 void DestroyIt () {
     if ( GameObject.Find("GlobalObjectName").GetComponent<Global>().Kill_it == true )
         Destroy(this) ;
 }

The second code:

 //Global.cs
 //This code controls all the prefabs
 
 public bool Kill_It = false ;
     
 void Update () {
 
     if ( Input.GetKeyDown(KeyCode.E) ) {
         Kill_It = true ;
     }
 }


Comment
Add comment · Show 1 · 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 Jeff-Kesselman · Dec 16, 2014 at 05:45 PM 0
Share

Hi Turtle. That is one way.

Another way is to give the master object a unique tag. This will get propagated to all clones. Then you use http://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html

Finally you can also use http://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html and look for instances of a component that only they share. Be warned however that FindObejctOfType is quite slow as it has to inspect your entire scene.

Id use the Tag method myself unless there is some reason you can't, like you area already using tags on those objects for some other set that is disjoint.

However there is nothing wrong with monitoring a global, if you've already implemented that. If you make it static then you don't have to make the code dependent on finding your global object.

 public class Destroy$$anonymous$$aster:$$anonymous$$onoBehaviour {
      static bool destroyNow = false;
 }
 
 public class DestroyClient:$$anonymous$$onoBehaviour {
      public void Update(){
         if (Destroy$$anonymous$$aster.destroyNow){
             Destroy(gameObject);
         }
     }
 }
avatar image
0

Answer by Deon-Cadme · Feb 16, 2014 at 09:33 AM

Hi,

"Null" is an expression for you tried to access and work with an empty position in RAM memory. Meaning, there was nothing there.

I would personally from experience try to do this a bit differently. I would have two scripts in my scene to handle this.

  1. A "group.cs" script that I place on an empty game object in the scene.

  2. A "alien.cs" script that I place on a prefab.

(The "groups.cs" script can also be on a prefab if you want to have several groups appearing in the scene at the same time and got a main game script somewhere)

The group script is responsible for managing the aliens in each group through a list or array. Just instantiate a group that then instantiates the number of enemies that you want. Just run through the list in the group with a for loop when you want to destroy them.

Comment
Add comment · Show 2 · 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 Deon-Cadme · Feb 16, 2014 at 09:34 AM 0
Share

ps. the group is also a perfect place to handle things like movement patterns and other behaviors that you might want for your enemies.

avatar image CodeTurtle · Feb 16, 2014 at 10:09 AM 0
Share

Hmm...I'm not sure if I can fully understand this. Do you mean that I should create several groups of certain number of aliens from the beginning. Then call them out depending on which number of enemies I want?

What I'm trying to comprehend is that why the function Destroy(gameObject) gives me a null reference error? I have attached the script under a prefab named "OtherCharacters2". So this error shouldn't have occurred.

However, ins$$anonymous$$d of using On$$anonymous$$ouseDown to trigger, using Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E) gives the exact solution I wanted, which is destroying all clones at once. I really want to know where the problem is ><.

avatar image
0

Answer by IlyaVorozhbit · Feb 16, 2014 at 11:01 AM

Hi.

I think you can to keep links to your NPC on some list, after creation, and when you'll be need to destroy them, just "follow" to that links, and do it.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

'parent' is not a member of 'Object'. 1 Answer

Battery script aint working. 0 Answers

Score Script Error? 1 Answer

Copy script not working 1 Answer

AddComponent() is adding 64 instances of my script to my GO 1 Answer


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