• 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 Geriath13 · Mar 05, 2014 at 02:28 AM · instantiateprefabclones

Assign same value to multiple instances of the same prefab

I made a grid of gameobjects from the same prefab that when one of those GO is clicked, it is destroyed and another gameobject (Pipe) is instantieted on the same position is that "position holder" GO.

The problem is: to know what kind of pipe is to be set, I assign a value to it. The "holder" GO is supposed to read it and instantiate that pipe on it's same position. Only the first instance of these "holders" is changing it's value, all others remain unchanged.

Also, when I put a pipe on that very first "holder" GO, the next one to it now changes it's value and I can manage to change it also. So basically, the script is somehow allowing me to change it sequentially but not in any random order I want.

     void OnGUI()
     {
         //SetPipe setPipeScript = GetComponent<SetPipe>();
         SetPipe setPipeScript = GameObject.FindGameObjectWithTag("Holder").GetComponent<SetPipe>();
         selGridInt = GUI.SelectionGrid(new Rect(50, 70, 100, 500), selGridInt, pipeShape, 1);
         {
             switch (selGridInt)
             {
                 case 0:
                     setPipeScript.pipeSelect = 1;
                     break;
 
                 case 1:
                     setPipeScript.pipeSelect = 2;
                     break;
 
                 case 2:
                     setPipeScript.pipeSelect = 3;
                     break;
 
                 case 3:
                     setPipeScript.pipeSelect = 4;
                     break;
 
                 case 4:
                     setPipeScript.pipeSelect = 5;
                     break;
 
                 case 5:
                     setPipeScript.pipeSelect = 6;
                     break;
 
                 case 6:
                     setPipeScript.pipeSelect = 7;
                     break;
             }
 
         }
     }

This is the GUI code I put in another GO, a GameManager.

 public class SetPipe : MonoBehaviour {
 
     public Transform pipe1; //T
     public Transform pipe2; //L Superior
     public Transform pipe3; //L Superior Invertido
     public Transform pipe4; //L Inferior
     public Transform pipe5; //L Inferior Invertido
     public Transform pipe6; //Vertical
     public Transform pipe7; //Horizontal
 
     public int pipeSelect;
     void Start () 
     {
 
     }
     
     void Update () 
     {
          
     }
 
     void OnMouseOver()
     {
         if (Input.GetMouseButtonDown(0))
         {
             switch (pipeSelect) //Alterado por método OnGUI em GameManager
             {
                 case 1:
                     Debug.Log("Cano T");
                     Instantiate(pipe1, gameObject.transform.position, Quaternion.Euler(0, 0, -90));
                     Destroy(gameObject);
                     break;
 
                 case 2:
                     Debug.Log("Cano L Inferior");
                     Destroy (gameObject);
                     Instantiate (pipe2, gameObject.transform.position, Quaternion.identity);
                     break;
 
                 case 3:
                     Debug.Log("Cano L Inferior Invertido");
                     Destroy(gameObject);
                     Instantiate (pipe3, gameObject.transform.position, Quaternion.identity);
                     break;
 
                 case 4:
                     Debug.Log("Cano L Superior");
                     Destroy (gameObject);
                     Instantiate(pipe5, gameObject.transform.position, Quaternion.identity);
                     break;
 
                 case 5:
                     Debug.Log("Cano L Superior Invertido");
                     Destroy (gameObject);
                     Instantiate (pipe4, gameObject.transform.position, Quaternion.identity);
                     break;
 
                 case 6:
                     Debug.Log("Cano Vertical");
                     Destroy (gameObject);
                     Instantiate (pipe6, gameObject.transform.position, Quaternion.identity);
                     break;
 
                 case 7:
                     Debug.Log("Cano Horizontal");
                     Destroy (gameObject);
                     Instantiate (pipe7, gameObject.transform.position, Quaternion.Euler(0, 0, -90));
                     break;
             }
         }
     }
 }

This is the code that set up the pipes. I tested it without the condition inside the mouse method and it actually works that way, but doing so will only allow one of the pipes to be placed (but it allows it to be placed anywhere and non-sequencially).

Took a couple of PS to help show it.

alt textalt text

These buttons on the side change the value of the Pipe Select.

Thanks for the help, guys. :D

ps02.png (442.0 kB)
ps01.png (446.7 kB)
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 srogee · Mar 05, 2014 at 04:57 AM

Your problem is that you aren't looping through the holders. You're only setting pipeSelect for the first one that FindGameObjectWithTag("Holder") finds.

You can also simplify your code quite a bit. Right now you have a lot of unnecessary repetition. You can simplify this even more by getting rid of the SetPipe script altogether and putting the Instantiate in your OnGUI function, but I assume you have a reason for doing it this way.

http://pastebin.com/8sEZdMGe

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 Geriath13 · Mar 05, 2014 at 09:53 AM 0
Share

Thank you very much, worked like a charm!

I did manage to clean it up a little bit, btw. Couldn't get rid of the SetPipe altogether yet, but working on it.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

How to follow multiple clones positions of an instantiate prefab having a velocity ? 1 Answer

Instantiate Prefab Array with ID 2 Answers

Instantiate is spawning too many clones 2 Answers

setting the text of an instantiated prefab's child's guitext object 1 Answer

Get unity to recognize prefab in C# 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