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

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.

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.

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.

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.

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. :slight_smile:

I don’t very understand your last answer.

If I understood well, I make a script for my menu that contain that :

var targetSphere : gameObject;
var meshToUse : MeshFilter;

targetSphere = GameObject.Find(“<your sphere’s name>”)

// on button click
targetSphere.GetComponent(MeshFilter).mesh = meshToUse;

After I use dontdestroyonload for what ?

You can use DontDestroyOnLoad(gameObject) to specify a gameObject to save when you load your next level. So you can use DontDestroyOnLoad(–your gameObject that holds your menu script–) so you can transfer your saved variables to the next scene.

Alternatively, you can use something like DontDestroyOnLoad(rememberScript.gameObject) and make a gameObject called ‘rememberScript’ with a script attached that only holds the MeshFilter you’ve selected.

No matter how you go about it, you’re going to want to do this:

  • Save your selected MeshFilter in a variable
  • Use DontDestroyOnLoad(gameObject) to save the gameObject that has your script attached that is holding onto your MeshFilter choice
  • Once your next scene loads, you’ll want to select your saved gameObject and access the MeshFilter
  • Place your MeshFilter you selected from your menu onto the gameObject you desire

Check the script reference page I posted for DontDestroyOnLoad to see how its used. You can simply call it from a Start() or Awake() function to be sure it won’t be deleted.

Hi everybody!
I’m an italian student and i’m using the mesh filter in my script to change the mesh of my object.
Now i’ve a simple object with the mesh of the Cylinder, present in the unity’s library.
I’ve understood that if I want to change the mesh i’ve to use this process:

var mesh : Mesh = new Mesh();
    GetComponent(MeshFilter).mesh = mesh;

In this case my object will disappear because variable mesh is empty. This is ok.

My 2 questions are:

1-How i can set my mesh value equal to the cube that is in unity’s default mesh?

2-How i can pass to my mesh variable the mesh of a 3DS model that i’ve imported in my assets folder? I suppose that i’ve to use Resources.Load("name.path"), but i’m not sure of this.

I hope that my english will be understandable.
Thanks in advance,