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 TR33 · Apr 25, 2017 at 02:31 PM · prefabobjectrandomenable

Enable and disable of prefab components

Hi Com,

I'm coding my first game and I have some troubble figguring out some code.
How the game works:
You(Player) can rotate an object to fit in some holes. Because it should be somewhat random I wanted to randomize it by instanciating a prefab with all the different holes and only activate one at a time. So with the help of the forum/manual I wrote some code but it works only partially (Instaciates always the same hole). Any idea how to fix it?

If a pic of my hirarcy might help you to figgure it out I will post one. But I wanted to keep this thread a bit smaller a first ;)

Thx :)

     public GameObject prefab;
 
     private int startLength = 5;
     public float spacing = 25f;
 
     private int randContainer = 0;
     private int randForm = 0;
     private int tmp = 0;
         
     public void Start()
     {
         //Random player generated
         RandContainer();
 
         //5 Random obstacles are generated in dependece of player form
         for (int z = 0; z < startLength; z++)
         {
             //Instanciate the prefab in a line with 25u spacing between
             Vector3 pos = new Vector3(0, 0, z) * spacing;
             Instantiate(prefab, pos, Quaternion.identity);
             //0 is only a placeholder to fix the problem I went into
             prefab.transform.GetChild(/*randContainer*/0).GetChild(RandForm(randContainer)).gameObject.SetActive(true);
             tmp = randForm;
             if (z < 0)
             {
                 //Disable the old one => only one Child is active
                 prefab.transform.GetChild(/*randContainer*/0).GetChild(tmp).gameObject.SetActive(false);
             }
         }
         //##Debugtools
         //Debug.Log("Container" + prefab.gameObject.transform.childCount);
         //Debug.Log("Form" + prefab.gameObject.transform.GetChild(0).transform.childCount);
         //randContainer = RandContainer();
         //RandForm(randContainer);
     }
 
     public void Update()
     {
 
     }
     
     public int RandContainer()
     {
         //Container = Cross or Star / pass the value to RandFrom()
         int randContainer = Random.Range(0, prefab.gameObject.transform.childCount + 1);
         Debug.Log("Container " + randContainer);
         return randContainer;
     }
 
     public int RandForm(int randContainer)
     {
         //Form = Cross1 or Cross2 / get the value from RandContainer()
         int randForm = Random.Range(0, prefab.gameObject.transform.GetChild(randContainer).transform.childCount + 1);
         Debug.Log("Form " + randForm);
         return 0;
     }
 }


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

Answer by TR33 · Apr 25, 2017 at 05:39 PM

Ok. Fixed it myself. After spending 3 days waiting for moderation :/

 public GameObject player;
 public GameObject prefab;

 private int startLength = 5;
 public float spacing = 25f;

 private int randContainer;
     
 public void Start()
 {
     //Random player generated
     randContainer = RandContainer();
     player.gameObject.transform.GetChild(randContainer).gameObject.SetActive(true);

     //5 Random obstacles are generated in dependece of player form
     for (int z = 0; z < startLength; z++)
     {
         //Instanciate the prefab in a line with 25u spacing between
         Vector3 pos = new Vector3(0, 0, z) * spacing;

         //Disable all the masks before instanciating one
         for (int i = 0; i < prefab.gameObject.transform.childCount; i++)
         {
             for (int j = 0; j < prefab.gameObject.transform.GetChild(i).transform.childCount; j++)
             {
                 prefab.gameObject.transform.GetChild(i).transform.GetChild(j).gameObject.SetActive(false);
             }
         }

         //Instanciating the mask and enable one mask 
         prefab.gameObject.transform.GetChild(randContainer).transform.GetChild(RandForm(randContainer)).gameObject.SetActive(true);
         Instantiate(prefab, pos, Quaternion.identity);
     }
 }
  
 public int RandContainer()
 {
     //Container = Cross or Star / pass the value to RandFrom()
     int randContainer = Random.Range(0, prefab.gameObject.transform.childCount);
     //Debug.Log("Container " + randContainer);
     return randContainer;
 }

 public int RandForm(int randContainer)
 {
     //Form = Cross1 or Cross2 / get the value from RandContainer()
     int randForm = Random.Range(0, prefab.gameObject.transform.GetChild(randContainer).transform.childCount);
     //Debug.Log("Form " + randForm);
     return randForm;
 }
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Random Object Spawn 0 Answers

How to activate a prefab power ups with different cubes?,Hi guys 0 Answers

How to set proper animation to prefabs randomly instantiated? 1 Answer

Adding components to prefab instead of player 0 Answers

Random object and random sprites? 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