- Home /
How to spawn a model with texture?
Hello people,
I'm working on this project and want to spawn a model with a texture when I press a button. I know how to use buttons and GUI. Just need help with spawning it and getting the texture on it.
Thanks
Ever since Code Sample fails for me;
function OnGUI () {
if (GUI.Button (Rect (5, 80, 100, 30), "Button"))
{ }
}
Answer by The_r0nin · Dec 12, 2010 at 09:06 PM
Put your material and the object you want to spawn in the following variables (in the Inspector window):
var mySpawn: GameObject;
var myMaterial: Material;
Then in your button function above (assuming you are spawning at myPosition and myRotation):
var newObject: GameObject = Instantiate(mySpawn,myPosition,myRotation);
mySpawn.renderer.material = myMaterial;
if you have trouble locating the material on the new object, you can also use GetComponent() or GetComponentInChildren().
You can make the object to spawn a prefab, and just spawn the prefab with the texture/material already on it as well...
Yeah, but I figured he wanted to apply the material at runtime (like a common mesh with a different material for allied or enemy... something like that). Your way is easiest, so long as every instance of that mesh has that one material.
Alright, I did as you said, which took me a lot of effort, but now I get the error that the variable mySpawn hasn't been assigned.
Help?
Drag the object you want to spawn into the mySpawn variable of the script in the Inspector.
Your answer
Follow this Question
Related Questions
How can I change an objects texture through javascript? 1 Answer
UV map not working 1 Answer
Random Spawner using radius and numbers 3 Answers
Exporting from cinema4d to unity3d 1 Answer