Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Ssiroo · Jun 13, 2014 at 04:54 PM · c#2djavascriptdestroyonmousedown

Script won't destroy prefab clones...

Hello ,I need some help making this script work :

 #pragma strict

 function OnMouseDown () {
 Destroy (gameObject);
 }

Basicly what the script does is that it destroys the objects that the player clicks on.I also got another object which has a script attached to it,which makes it spawn other objects.

The script clones the objects and makes it spawn multiple times.I attached the first script to the prefab but when I click on it,it won't be destroyed.If I click on the original prefab it works,but not on the clones created by the other script.

Anyone has any idea ?

Thank you.

Edit#1 :

I belive the problem doesn't stand at the "destroyer" script,because the Debug.Log I added OnMouseDown won't show up.

My spawner script :

 using UnityEngine;
 using System.Collections;

 public class Spawner : MonoBehaviour {
 
 public GameObject[] obj;
 public float spawnMin = 1f;
 public float spawnMax = 2f;
 
 
 // Use this for initialization
 void Start () {
     Spawn ();
 }
 
 void Spawn() 
 {
     Instantiate(obj[Random.Range(0, obj.GetLength(0))],transform.position, Quaternion.identity);
     Invoke ("Spawn", Random.Range (spawnMin, spawnMax));
 }
 
 }


BIG UPDATE #1(fix) : I removed the background (2D quad with a texture behind the objects) and all the scripts seem to be working fine,no idea why.Thanks to everyone who tried to help,I realy appreciate it.Such a nice community!

Thank you.

Comment
Add comment · Show 6
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 tanoshimi · Jun 13, 2014 at 07:15 PM 0
Share

When the game is running, select a clone in the hierarchy and look at the inspector pane - does it definitely have this script attached to it?

avatar image robertbu · Jun 13, 2014 at 07:17 PM 0
Share

On$$anonymous$$ouseDown() requires the object to be the first collider hit. Any chance there is another collider between the mouse and the clicked-on object?

avatar image Ssiroo · Jun 13, 2014 at 08:04 PM 0
Share

@tanoshimi - Yes,it does have the script attached to it. @robertbu - The cloned object has only a circle collider (2D) attached to it and there isn't anything between the mouse and the object that's being clicked on.

avatar image Ssiroo · Jun 14, 2014 at 11:14 AM 0
Share

The clones that appear on the hierarchy are white,which means that they don't appear as the prefab,I don't know if that matters.

avatar image Ssiroo · Jun 16, 2014 at 07:23 PM 0
Share

Still looking for some help,unfortunately.

Show more comments

1 Reply

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

Answer by PommPoirAbricot · Jun 17, 2014 at 12:09 PM

Try that also

 GameObject[] all = GameObject.FindAllGameObjects("yourGameObjectName" + "(Clone)");
 
 foreach(var g in all)
     Destroy(g);

Comment
Add comment · Show 6 · 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 HarshadK · Jun 17, 2014 at 12:20 PM 0
Share

I believe, this will destroy all game objects that are clone of the prefab and not the only one which is clicked by the user. Later is the requirement of the OP.

avatar image Ssiroo · Jun 17, 2014 at 12:43 PM 0
Share

I would guess so,also unity won't recognise FindAllGameObjects as a definition.

This is the error it gives me : Assets/Scripts/BalloonD.cs(7,39): error CS0117: UnityEngine.GameObject' does not contain a definition for FindAllGameObjects'

And this is the complete script (Which I may have edited it wrong) :

 using UnityEngine;
 using System.Collections;


 public class BalloonD : $$anonymous$$onoBehaviour {

 GameObject[] all = GameObject.FindAllGameObjects("yourGameObjectName" + "(Clone)");

 void On$$anonymous$$ouseDown () {

     foreach(var g in all)
         Destroy(g);
 }
 }


I also added a Debug.Log in my js version of this and I figured out that the function is not being called on click.

avatar image PommPoirAbricot · Jun 17, 2014 at 12:54 PM 0
Share

ok try that

 using UnityEngine;
 using System.Collections;
 using System.Linq;
 public class BalloonD : $$anonymous$$onoBehaviour
 {
     private GameObject[] all;
     // Use this for initialization
     void Start()
     {
         all = GameObject.FindObjectsOfType<GameObject>().Where<GameObject>(g => g.name.Contains("yourGameObjectName" + "(Clone)")).ToArray();
     }
     void On$$anonymous$$ouseDown()
     {
         foreach (var g in all)
             Destroy(g);
     }
 }
 



avatar image Ssiroo · Jun 17, 2014 at 01:01 PM 0
Share

No errors now,I attached a Debug.Log to it and noticed that it's not being called,I think the real issue is at my spawner script,at the way they're being spawned.I also added some information to the question.

avatar image PommPoirAbricot · Jun 17, 2014 at 01:12 PM 0
Share

Otherwise if you only want to delete one clicked cloned gameobject, try when intantiating your GameObject, to attach a collider component and this script:

     public class BalloonD : $$anonymous$$onoBehaviour
     {
         void On$$anonymous$$ouseDown()
         {
                 Destroy(this.gameObject);
         }
     }
 

Use this script to instantiate :

     public class patatipatata : $$anonymous$$onoBehaviour
     {
         public GameObject myGameObjectToClone;
     
         void OnGUi()
         {
             if (GUILayout.Button("clone me"))
             {
                 GameObject  tmp = Instantiate(myGameObjectToClone, Vector3.zero, Quaternion.identity) as GameObject;
                 tmp.AddComponent<Collider>();
                 tmp.AddComponent<BalloonD>();
             }
         }
     }





Show more comments

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

6 People are following this question.

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

Related Questions

2D C# destroy a GameObject on collision 2 Answers

2D games; Javascript or C#? 1 Answer

Erasing a gameobject if you have the coordinates. 3 Answers

Access a JavaScript variable from a C# script? 2 Answers

destroying gameobject from other script 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