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 maroltl · Apr 14, 2014 at 08:16 PM · instantiatetagtags

Instantiate prefab with different tag

Is it possible to instantiate same prefab but with different tag based on position in game. Like if prefab(clone) x position is between 0 and 64 it would have tag "first", prefab(clone) between 64 and 128 would have tag "second"..

I'm using for loop for instantiating prefabs. It just instantiate cubes one beside other (128* 16) length and width.

Some tips on how could I achive that if it is even possible would be appreciated.

Comment
Add comment · Show 1
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 rednax20 · Apr 14, 2014 at 08:42 PM 0
Share

dang it! those two beat me to it while i was typing my answer! good job with the quick replies.

4 Replies

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

Answer by rednax20 · Apr 14, 2014 at 08:41 PM

sure, you can access a prefab after you instantiate it by setting it's value to another gameobject

 var clone : GameObject; //you don't need to asign this to anything in the inspector
 
 for(var i = 0; i < 128; i++){ //i'm assuming this is how you are doing it, if not just apply the clone accessing to your code
 
           if(i < 65){
                     clone = Instatantiate(prefab, Vector3(0,0,0), Quaternion.Identity
                     clone.tag = "first"
           }
           if(i > 64){
                     clone = Instatantiate(prefab, Vector3(0,0,0), Quaternion.Identity
                     clone.tag = "second"
           }
 
 
 }

this give sort of an example of what you want to do, except that person wanted to get a component while you just wanted to change the tags of the object

good luck!

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 tolfer · Aug 21, 2017 at 07:07 PM 0
Share

What I do Wrong ? I have code and code doesnt tagged new object until i didnt have tagged my prefabs. Why ?

  for (int y = 1; y < 2; y++)
 {
     for (int x = 0; x < 6; x++)
     {

         GameObject block = Instantiate(blocks, new Vector2(x, y), Quaternion.identity) as GameObject;
         block.transform.parent = this.transform;

         GameObject positionOfNumber = block.transform.Find("PositionOfNumber").gameObject;

         GameObject number = Instantiate(numbers[x], positionOfNumber.transform.position, Quaternion.identity) as GameObject;
         number.transform.parent = block.transform.GetChild(0) ; // rodic objektu number bude dieta objektu block
   
         block.gameObject.tag = x.ToString(); //otagovanie objektu
     }
 }

}

avatar image
0

Answer by robertbu · Apr 14, 2014 at 08:34 PM

Just assign the tag at runtime. Something like:

    var go : GameObject = Instantiate(prefab, pos, Quaternion.identity);
 
    if (pos.x < 64) { 
        go.tag = "first";
    }
    else if (pos.x < 128) {
        go.tag = "second";
    }
    else {
        go.tag = "third";
    }

Note your position values sound like they might be screen positions. If so, you'll need to convert the world position for the instantiate into a screen position using Camera.WorldToScreenPoint().

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
avatar image
0
Wiki

Answer by MasterPDON · Apr 14, 2014 at 08:35 PM

Remember that you can change the tag of Game Objects during runtime.

  public GameObject newPrefab;
     public GameObject prefab;
     void Start(){
     newPrefab = (GameObject)Instantiate (prefab, transform.position, Quaternion.identity);
     newPrefab.tag = "NewTag";
     }
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
avatar image
0

Answer by trane294 · Apr 14, 2014 at 08:47 PM

You can do gameObject.tag or change prefab(clone) name gameObject.transform.name

     gm = Instantiate(prefab, prefab.transform.position, Quaternion.identity);
     gm.tag = "second";
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.

Follow this Question

Answers Answers and Comments

24 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 avatar image avatar image avatar image

Related Questions

instantiate a random object from multiples via 'tag' 1 Answer

Prefab tag bug 0 Answers

Are Tags the best solution for this? Duplicating an item that duplicates itself a set amount of times. 1 Answer

Instantiate an object correctly 1 Answer

Having problem with making non networked objects only be intractable with local player 0 Answers


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