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 rajeev777 · Apr 11, 2016 at 02:36 PM · unity 5gameobjectscript.

Multiple GameObjects with same Script

I have a prefab which is instantiated in the scene ( 6 copies of prefab). I am trying to disable it with a touch on it , but when i tap on it it disables all the gameObjects of the prefab. I am using single Script to destroy it.

i can't figure what is happening. Can anyone check what 's wrong i am doing here....

 void Update ()  {


 for (int i = 0; i < Input.touchCount; i++)
 {
         if (Input.GetTouch (i).phase == TouchPhase.Stationary) {
                   hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (Input.GetTouch (i).position),      Vector2.zero);
 
         if (hit.collider != null && hit.transform.gameObject.tag == "BlackSquare")
                         ClickedDown (hit.collider);
 
 
 }
 
 
 }
 
     void ClickedDown(Collider2D collider){
 
              Instantiate (blastEffect,transform.position,transform.rotation);
 
         this.gameObject.SetActive (false);
 
         waitForSecondsCounter = waitForSeconds;
 
         Color c = this.transform.GetComponent <SpriteRenderer>().color;
 
         c.a = 1f;
 
 
         this.transform.GetComponent <SpriteRenderer> ().color = c;
 
 
 }
 
 

 

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Redwolve · Apr 12, 2016 at 03:38 AM

@rajeev777

Your problem appears to be that you are setting a conditional in the Update() that makes perfect sense. However, by using

this.gameObject.SetActive(false);

instead of

collider.gameObject.SetActive(false);

you are essentially telling every prefab "hey you saw this object get touched so disable yourself", you are not telling the script to disable the object attached to the collision rather all of the objects with this script.

That should do the trick, happened to me in the past as well. Please let me know if it works out for you!

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 rajeev777 · Apr 12, 2016 at 08:24 AM 0
Share

@Redwolve you are saying correct and it works perfectly but when collider is null , then what to do in that case?

like when i am setting gameObject.SetActive(false); it is applied to al the prefab instances;

Here is my code

 if (collider != null) {
 
             Instantiate (blastEffect,collider.transform.position,collider.transform.rotation);
 
             GameObject obj = collider.gameObject;
 
             obj.SetActive (false);
 
             Color color = obj.GetComponent <SpriteRenderer> ().color;
 
             color.a = 1f;
 
             obj.GetComponent <SpriteRenderer> ().color = color;
 
             waitForSecondsCounter = waitForSeconds;
 
         
         
         } else {
         
 
             Instantiate (blastEffect,transform.position,transform.rotation);
 
             gameObject.SetActive (false);
 
             Color c = sprite.color;
 
             c.a = 1f;
 
             sprite.color = c;
 
             waitForSecondsCounter = waitForSeconds;
 
 
 
         
         }


avatar image Redwolve · Apr 12, 2016 at 12:26 PM 0
Share

@rajeev777

What are you wanting to happen different when the collider is not detected? Your code looks about the same for if and for else

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Destroy(this.gameObject) destroys all the obejcts in the scene, i want only the one that is clicked on 1 Answer

GetComponent is not a member of Object 0 Answers

Enable/Disable other gameobject's scripts Unity 5 0 Answers

Reset rotation of gameobject 1 Answer

Make a object always appear behing another object 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