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 mouz13 · May 18, 2021 at 10:43 AM · scripting problemuidropdownspawning problems

Spawn meshes to scene via dropdown UI button

Hello. I am a new programmer and i want your help. I am working a project where i try to select and spawn a primitive mesh in scene via a dropdown button. I never use something like that so if anyone can help me. I have made an array wich i include my primitives meshes and i want to select them when i select the dropdown options. Here is my code:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class ChangeMeshFilter : MonoBehaviour { public Mesh[] myMesh= new Mesh[5]; private int a = 0; private GameObject gObject; public Dropdown MeshSelector;

 void Start () 
 {
     CreateObject();
  
 }

 void CreateObject() {
     gObject = new GameObject();
 
     gObject.AddComponent<MeshFilter>().mesh = myMesh[0] ;
     gObject.AddComponent<MeshRenderer>();
     gObject.GetComponent<MeshRenderer>().material.color = Color.green;
 }
 
 public void Changer() 
 {
     if (a < 4)
         {
             a = a + 1;
         }
         else
         {
             a = 0;
         }
 
         gObject.GetComponent<MeshFilter>().mesh = myMesh[a];
 }
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

2 Replies

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

Answer by Hellium · May 18, 2021 at 10:58 AM

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class ChangeMeshFilter : MonoBehaviour
 {
     public Mesh[] myMesh= new Mesh[5];
     private GameObject gObject;
     public Dropdown MeshSelector;
  
     void Start () 
     {
         CreateObject();
         MeshSelector.onValueChanged.AddListener(SelectMesh);
     }
 
     void CreateObject() {
         gObject = new GameObject();
  
         gObject.AddComponent<MeshFilter>().mesh = myMesh[0] ;
         gObject.AddComponent<MeshRenderer>();
         gObject.GetComponent<MeshRenderer>().material.color = Color.green;
     }
  
     private void SelectMesh(int index) 
     {     
             gObject.GetComponent<MeshFilter>().mesh = myMesh[index];
     }



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 mouz13 · May 18, 2021 at 11:44 AM 0
Share

Hi and thanks for you time. So if i understand right i must use this script to my script? Or i must you 2 different scripts

avatar image Hellium mouz13 · May 18, 2021 at 11:49 AM 0
Share

The script I've provided is meant to replace yours.

avatar image mouz13 Hellium · May 18, 2021 at 12:41 PM 0
Share

You are the best my friend. Really thanks you. A last question. If i want to select the type of the mesh with the dropdown button, but i want to span this mesh on screen with an other one? is it possible?

Thanks for your time again

Show more comments
avatar image
0

Answer by Adil_Alhilali · May 18, 2021 at 11:02 AM

Hi,

Could you please clarify if you want to have an inspector attribute with a dropdown via which you select the type of mesh you want to spawn in your scene or you wan this function to be during run-time in UI?

if you want to make this in the inspector then do this

 enum DDLEnum
  {
     Item1, 
     Item2, 
     Item3
  };
  var dropDown = DDLEnum.Item1; 


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 mouz13 · May 18, 2021 at 11:09 AM 0
Share

Hello and thanks for your time. I would like to make it work in runtime

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

286 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 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 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 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 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 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 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 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 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 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 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 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

Creating a dynamic dropdown menu based on active GameObjects 0 Answers

Get Dropdown Index value based on text value using Where c# filter 1 Answer

How can i make dropdown menu which opens within Menu? 0 Answers

OnPointerClick in child's script is not triggered when there exists a OnPointerDown in parent's script 2 Answers

Cannot edit appearence of dropdown UI Unity 5.6 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