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 SUPPEAR · Feb 28, 2015 at 05:23 AM · android2dgameobjectobjectmobile

Duplication Problem. Please help.

Hello. I am fairly new to both Game Creation and Scripting. I had created a script that duplicated my set object. I also had another script that destroys the object when the player collides with the object. All goes well until the duplication part. When the object becomes duplicated it changes its name from "Apples" to "Apples(clone)" When it adds the "(clone) to the end it doesn't allow my destroy on collision script to work since the name isn't "Apples" This is the script using UnityEngine; using System.Collections;

 public class DestroyApples : MonoBehaviour {
 
     void OnCollisionEnter2D (Collision2D col)
     {
         //Check collision name
         Debug.Log("collision name = " + col.gameObject.name);
         if(col.gameObject.name == "Apples")
         {
             Destroy(col.gameObject);
         }
     }
 }

Is there a way it can be duplicated without the "(clone)" or a way that it allows the script to detect the object.

Sorry if my writing is in poor condition. I am quite tired. If any questions pop up, please ask.

All help is very much appreciated. Thank you.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by alok-kr-029 · Feb 28, 2015 at 05:48 AM

try to store the gameobject in a local variable
eg -

  Gameobject gObj = (Your Instantiate code )as GameObject;
 gObj.transform.name ="Apples";

This will work

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 SUPPEAR · Feb 28, 2015 at 08:33 PM 0
Share

Thank you for the reply. So where you have said (Your Instantiate Code) I would put what in there?

avatar image meat5000 ♦ · Feb 28, 2015 at 09:07 PM 0
Share

In the place you call instantiate to duplicate your object, ins$$anonymous$$d of using

 Instantiate(something, byThere, thatWayRound);

use

 GameObject myClone = Instantiate(something, byThere, thatWayRound);

Then you have a reference to the instantiated object, with which you can refer to its name and change it, as stated above.

@alok, the .transform. is an unnecessary step. gObj.name is fine.

avatar image
0

Answer by maccabbe · Feb 28, 2015 at 06:37 AM

You can also destroy based on the tag of the enemy. Set the tag of the apple prefab to a new tag, "Apples" then use the following

 if(col.gameObject.tag== "Apples"){
      Destroy(col.gameObject);
 }
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 videoanime · Feb 28, 2015 at 08:13 AM 0
Share

Or try too the layer to destoy it

 void OnCollisionEnter2D(Collision2D coll)
         {    
         if (coll.gameObject.layer == 9)
                 Destroy(coll.gameObject);
         }

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

20 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

Related Questions

Transform.localPosition Script Problem. 3 Answers

Collision On Object Error. Please help. 1 Answer

Problem creating a script to destroy object on collision. 1 Answer

Transform.localPosition Scripting Problem. 1 Answer

Expand object on touch? 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