- Home /
Mesh Filter
I want to change the mesh of my object by script. I have different object that the player will can choose with a menu.
How I can change the mesh filter by script. Like if the player press B change the mesh filter by Ball 2 instead of ball 1
@ovechkin1308 and @Retribution : try to keep your comments out of the answer area or attached to the answer that they're referencing. If you need to change the body of an answer, you can edit it.
At the moment this question's thread is very tough to follow and that is likely to hamper anyone else trying to understand what's going on.
Sorry ChrisD. I'm new to Unity Answers, but I'll try to keep the posts cleaner in the future.
No worries. Everyone was new at some point and, unfortunately, there isn't really a FAQ or guide to posting up for the new site yet :(
Answer by Retribution · Jun 07, 2011 at 08:59 PM
You should be able to do something like this:
var go : gameObject;
go.GetComponent(MeshFilter).mesh = Resources.Load("<enter path here>");
Where 'go' is the gameObject you wish to access. You can use Resources.Load() and enter the path to the mesh in your project, or you could store the mesh in a variable that you could set through the inspector. For that, you would have something like:
var go : gameObject;
var meshToUse : MeshFilter;
go.GetComponent(MeshFilter).mesh = meshToUse;
Where meshToUse is setup with a mesh in the inspector and go is the gameobject you want to access.
Answer by ovechkin1308 · Jun 08, 2011 at 01:46 PM
It's doesn't work. My principal script is on the sphere object that I want to choose the mesh filter. So I want that the player can choose the mesh filter he want.
Forgot to include this in my post:
If you're copying this code into your project, it won't work right away. You'll have to change some of the code (like the path to your materials, or setup the $$anonymous$$eshToUse in your inspector) to get it to work.
If you're getting null reference errors for the 'meshToUse' variable, its because you will need to drag a $$anonymous$$eshFilter object into that variable under the script component.
Answer by Retribution · Jun 09, 2011 at 12:02 AM
Ovechkin1308, do you get errors when you try this method? Or are you saying you can't accessing the object you want to change from your script location? Post some more details for me so I can try to understand your current issue.
To me, It sounds like you're saying: You have a menu that has selections of which mesh the player wants to use, and then you want to change the mesh on your sphere object when the player clicks one of the buttons. If this is the case, you should be able to do something like this:
var targetSphere : gameObject;
var meshToUse : MeshFilter;
targetSphere = GameObject.Find("<your sphere's name>")
// on button click
targetSphere.GetComponent(MeshFilter).mesh = meshToUse;
If you're only having issues accessing your sphere object from your menu, you can use a method like this. By using GameObject.Find(), you can target your sphere object from your menu and change its MeshFilter based on what you select from the menu.
If this isn't your issue, please post more details as I've misunderstood your question.
Answer by ovechkin1308 · Jun 09, 2011 at 04:20 PM
So I will have a scene wich is the menu of the sphere choice. So it will have like 1 gui button for each type of sphere. when the player will click on the type of sphere he want the first level will load and I want that the sphere gameobject of my scene are of the right type.
Answer by Retribution · Jun 09, 2011 at 09:00 PM
Overchkin1308, I suggest looking at this part of the documentation for what you're doing: DontDestroyOnLoad. This allows you to save an object upon loading into the next scene, which will allow you to save your MeshFilter choice.
Attach your script that will be selecting your MeshFilter choice from your menu selection and use DontDestroyOnLoad to prevent this script from being destroyed upon loading into your next level. Once your next level as loaded, grab the MeshFilter you've stored in your script and assign it to the sphere in your level.
This should give you all the functionality you need to swap your MeshFilter out at the start of your first level. :)