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 /
avatar image
0
Question by kkfarrel · Jan 27, 2019 at 04:16 AM · gameobjectmeshonmousedown

Change Mesh/Gameobject onmousedown?,Swap mesh/gameobject randomly onmousedown

Hi,

I am pretty new to Unity and was wondering if anyone could help me figure out how to randomly change the current mesh/gameobject into a different one with each click? So I have a default mesh, when I click it, I want it to change into the head of a politician (I have a few different politician heads, and I want to be able to randomly switch to a different head on each click. Even better, have it change back to the default mesh in between. For example: Starts as default, click-politician1, click-default, click-politician5, click-default, etc.

I have a starting code for the mousedown, I just don't know what to put inside of it. Please help!

 public class ChangeMesh : MonoBehaviour {
 
 
     void OnMouseDown()
     {
         // Change mesh to random politician when you click
         
     }
 }    
 

Comment
Add comment · Show 7
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 DCordoba · Jan 27, 2019 at 05:13 AM 1
Share

hum, to change a mesh you need to access the $$anonymous$$eshFilter component to a gameobject, and then to the $$anonymous$$eshRenderer to change the material, or materials, to each one, so I think is better to store a array of prefabs with each head, and randomly choose a head, you need to put this on the hierachy position to the default head so so it will make some correlation to the body/bones modifiers, also you can do personalizations for each head, like movements etc, just changing the mesh can become a problem fast...

so lest store a array of prefabs, but, ins$$anonymous$$d of creating/destroying the default, we will store both, just two heads, the current and the most used will be stored

 public class Change$$anonymous$$esh : $$anonymous$$onoBehaviour {
      public GameObject[] politicians; //array of prefabs with heads
      public GameObject defaultHead; //the current head, present on scene will be disabled when other head is present
      GameObject currentHead = defaultHead; 
 
  
      void On$$anonymous$$ouseDown()
      {
          // Change mesh to random politician when you click
          if(defaultHead.activeSelf){
               defaultHead.SetActive(false);
               int randInt = Random.Range(0, politicians.Length);
               currentHead = Instantiate(politicians[randInt], defaultHead.transform.position, defaultHead.transform.rotation, defaultHead.transform.parent);
           }else{
               Destroy (currentHead);
                defaultHead.SetActive(true);
               currentHead = defaultHead;
           }
      }
  }


avatar image kkfarrel DCordoba · Jan 27, 2019 at 06:46 PM 0
Share

Thanks! I tried this, but got this error: A field initializer cannot reference the nonstatic field, method, or property `SwapHeads.defaultHead'

Also just want to clarify that these heads are not attached to bodies and are not doing anything. they are simply floating heads that I want to swap out as you click. I tried attaching the script to a button- I don't know if that's what messed it up?

When I tried to make the array, it wouldn't let me add the objects because of the compile error.

avatar image DCordoba kkfarrel · Jan 27, 2019 at 07:12 PM 0
Share

delete

   GameObject currentHead = defaultHead; 

just leave it without initialize it

   GameObject currentHead;

this can't cause problems but if you want do it robust initialize it on start

 void Start(){
       currentHead = defaultHead; 
 }

with this I think you can initialize it on inspector

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

153 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

Related Questions

Add mesh with materials to scene through code. 0 Answers

How to mark the vertices on a mesh and make the markers follow them as you apply transformations 0 Answers

Attaching a model to a blank GameObject? 0 Answers

Why the guismo of the GameObject is at the center but its "mesh" is not at all in the center? 0 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 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