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 Superdsgross · Apr 21, 2015 at 02:36 PM · c#instantiatetag

Instantiate a child with its tag

I've looked around at all the forums I can regarding this and I'm still stumped. My goal is to instantiate a field (with a bunch of objects including 3 player spawn tiles), click on a gui button and have a character spawn on P1T (if its available), then spawn two other characters on the other two tiles.

I've got quite a few levels that all sit as prefabs in the same scene. Its important that the objects are instantiated because of how the level progression works. However when I instantiate one of the fields the child objects lose their tags. And when I tried to create a script on the tiles to have them change their own tags when they Awake, the scripts aren't even components once the parent object is instantiated.

Does anybody know how I could assign them tags after they're instantiated or have the tags stay?

And once I'm able to spawn a character is there any way I could do a simple- if tag P1 exists on the hierarchy check for P2?

public class OnCharButtonClick : MonoBehaviour {

 public GameObject PlayerPrefab;
 private GameObject P1Tile;
 

 public void ChooseChar(int level)
 {
     // button turns grey
     // button becomes disabled
     // Instantiate PlayerPrefab
     // Check if tag P1
     // if yes check if P2
     // if yes check if P3
     // if yes debug too many characters
     // if no change PlayerPrefab tag to P3
     // -Close Panel
     // if no change to P2
     // if no change to P1

         P1Tile = GameObject.FindWithTag("P1T");

     Instantiate (PlayerPrefab,P1Tile.transform.position,Quaternion.identity);

     
 }

}

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
0
Best Answer

Answer by hbalint1 · Apr 21, 2015 at 09:37 PM

try this:

 GameObject newObject = (GameObject)Instantiate (PlayerPrefab,P1Tile.transform.position,Quaternion.identity);

 newObject.tag = "P1T";
Comment
Add comment · Show 3 · 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 Superdsgross · Apr 21, 2015 at 10:59 PM 0
Share

On which script should I attach that? I instantiate the objects using a loader script that instantiates a random tree of objects:

using UnityEngine; using System.Collections;

public class Loader : $$anonymous$$onoBehaviour {

 public GameObject Forest1;
 public GameObject Forest2;
 public GameObject Forest3;

 public int raid1$$anonymous$$;
 public int raid1max;


 public bool newStage;
 public bool raidOver;
 private int randomLvl;
 private int random$$anonymous$$ob;



 // Use this for initialization
 void Start () {
     newStage = true;
     raidOver = false;
 
 }
 
 // Update is called once per frame
 void Update () {

         if(newStage==true)
     {
         randomLvl=Random.Range(raid1$$anonymous$$, raid1max);
         random$$anonymous$$ob=Random.Range(1,3);
         Destroy(GameObject.FindWithTag("RoomObjects"));
             if(randomLvl==1)
         {
                 Instantiate (Forest1);
         }
         if(randomLvl==2)
         {
             Instantiate (Forest2);
         }
         if(randomLvl==3)
         {
             Instantiate (Forest3);
         }
         newStage=false;

             

     }

     if (raidOver == true) {
         Application.LoadLevel(0);

     }
 
 }

}

avatar image hbalint1 · Apr 22, 2015 at 08:39 AM 0
Share

sorry, i now read it again and i misunderstood the question. so somehow only the children lose their tags. taht shouldn't be happen. if you set the children's tag correctly, then make prefab from the whole hierarchy they should keep their tags

avatar image Superdsgross · Apr 22, 2015 at 11:58 AM 0
Share

You're right. It was something stupid :P I edited the prefab in from the project folder ins$$anonymous$$d of the hierarchy. For anyone who runs into this issue in the future, you need to have the prefab in the hierarchy, then delete the original and replace it with your new one.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Instantiated clones arent behaving the same as the prefab 1 Answer

Distribute terrain in zones 3 Answers

Instantiate prefab (button, texture) with attached script 1 Answer

Projectile not moving properly in top down 2d shooter 2 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