- Home /
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
}
}
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;
}
}
}
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.
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
Your answer
Follow this Question
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