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
1
Question by danipren · May 01, 2014 at 11:01 PM · spawnprefabsclones

How could I diferentiate between clones?

Hi everyone, ive got stuck on a piece for the game im working on

first of all the problomatic code
part 1

 function OnGUI () 
     {
         
         
         if(isincombat)
             {
                 GUI.Label (Rect (700, 10, 500, 300), "" + enemymonster.name);
                 GUI.Label (Rect (700, 30, 500, 300), "" + enemymonster.baseHP + "/" + enemymonster.currHP);
                 GUI.DrawTexture (Rect(900, 130, 128, 128), enemymonster.image);
                     if(spawned == false) 
                         {
                             Instantiate(enemymonster.gamemodel,enemymonsterspawn.transform.position,enemymonsterspawn.transform.rotation);
                             spawned = true;
                         }
                 GUI.Label (Rect (10, 10, 500, 300), "" + equippedmonster.name);
                 GUI.Label (Rect (10, 30, 500, 300), "" + equippedmonster.baseHP + "/" + equippedmonster.currHP);
                 GUI.DrawTexture (Rect(70, 10, 128, 128), equippedmonster.image);
                 GUI.Label (Rect (10, 50, 500, 300),"Level " + equippedmonster.level + " XP " + equippedmonster.curXp + " / " + equippedmonster.maxXp);
                 if(spawned2 == false) 
                     {
                         Instantiate(equippedmonster.gamemodel,mymonsterspawn.transform.position,mymonsterspawn.transform.rotation);
                         spawned2 = true;
                     }
                 
                 if (!spawned)
                     {
                         Destroy(GameObject.Find(enemymonster.name + "(Clone)"));
                     }
                 if (!spawned2)
                     {
                         Destroy(GameObject.Find(equippedmonster.name + "(Clone)"));
                     }
      if (!displaygui) return;



 if(monstersub)
         {    for(var k=0; k < 6; k++)
                 {
                     
                     if(GUI.Button(Rect(700,150 + (k * 50),100, 30),"" + mymonsters[k].name))
 
                 {
                     Destroy(GameObject.Find(equippedmonster.name + "(Clone)"));
                     equippedmonster = mymonsters[k];
                     if(spawned2)
                     {
                     Instantiate(equippedmonster.gamemodel,mymonsterspawn.transform.position,mymonsterspawn.transform.rotation);
                     }    
                 }
             
             }

now the problem im facing is that way im despawning the monsters by finding by name + clone is fine but when the players equipped monster is has the same name as the enemy monster there is no way of telling which one to destroy and it generally destroys the wrong one.

Oh the monsters are an array containing the gamemodels stats etc

hope im making sense and would appreciate any help or maybe a better way of placing and destroying prefabs runtime.

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 robertbu · May 01, 2014 at 11:17 PM 0
Share

I don't completely understand your setup here, but you can change the name or the tag of a game object. So I suggest you give your enemy monsters and your player monsters different tags or names:

  var go = Instantiate(equippedmonster.gamemodel,mymonsterspawn.transform.position,mymonsterspawn.transform.rotation) as GameObject;
  go.name = "SomeName";
  go.tag = "SomeTag";

avatar image danipren · May 03, 2014 at 12:52 AM 0
Share

thanks robert sorry i didnt see your reply sorry lol

1 Reply

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

Answer by ChristianLinnell · May 01, 2014 at 11:31 PM

I'm not 100% sure I understand your game mechanic, but I'm imagining a Pokemon style game. If that's right, there are two ways to do this.

Local variables

Instead of Instantiating the monsters and then searching for them, have your script store what gets created.

At the top of your script, create two new GameObject variables: equippedMonster and enemyMonster. Then, when you instantiate them, do this:

 equippedMonster = Instantiate(equippedmonster.gamemodel,mymonsterspawn.transform.position,mymonsterspawn.transform.rotation);

Then when you go to despawn, you can refer to them directly:

 Destroy(equippedMonster);



Unique IDs

If you can't do that for some reason, you could store the unique instance ID of each of your spawned monsters.

 spawnedMonster.GetInstanceID();

Easy :-).

Comment
Add comment · Show 4 · 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 danipren · May 02, 2014 at 01:09 AM 0
Share

thanks for the quick reply christian,

sorry im new at scripting so bare with me.

i tried the first way which while it did make my code tidier it was doig the same thing it was retrieving the name+clone from equipped$$anonymous$$onster but when it dstroys it finds the first name+clone in the heirarchy and destroys that one which is usually the enemy.

on the unique ids i get what they are but no how to use them any tips?

EDIT Never $$anonymous$$d you were right the first time just me rushing ahead o myself and missed some changes in the script

always up for learning if someone waned to explain how id go about it with Get.InstanceID();

avatar image ChristianLinnell · May 02, 2014 at 04:19 AM 0
Share

Good to hear it's working! Don't forget to accept the answer to help others see the solution :-).

The GetInstanceID() thing is much less helpful, but it's good to be aware that everything in your scene has a unique ID.

Basically if you had to use GameObject.Find() for some reason, you could rename objects you create to match their unique ID, and then store that somewhere, just like how you stored the GameObject you instantiated. Like this:

 //Create the object
 o = Instantiate(whatever);
 
 //Rename it
 o.name = o.GetInstanceID().ToString();
 
 //Store the name
 var storedName = o.name;
 
 //Then, later
 GameObject.Find(storedName);

It's really not the best way to do it, but it's good to be aware of it. $$anonymous$$ost of the time you can use the method you've just gotten working.

avatar image danipren · May 02, 2014 at 10:55 AM 0
Share

ah i see, i found the object id's one day when i switched to debug and thought ah ha that could come in handy. but could not figure it out thanks for the rpelys christian you have been super helpful. ill accept the answer now or as soon as i figure out how lol

avatar image ChristianLinnell danipren · May 03, 2014 at 12:30 AM 0
Share

Little tick box next to the upvote button :-)

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

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

Problem: Random Instantiating more than one prefab? 1 Answer

Spawn Random Objects on a Sphere Surface 0 Answers

Spawn various enemies at random 1 Answer

How to manually handle multiplayer prefabs spawning with our own script ? 0 Answers

Unity2d : Spawning in Prefabs 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