Adding a MeshFilter mesh via script? - c#

How do you add a MeshFilter component with a specific mesh, let's say a Capsule mesh (from Unity default library resources), via C#? I'm this far ...
GameObject obj = new GameObject("Player");
MeshFilter meshFilter = obj.AddComponent<MeshFilter>();
meshFilter.mesh =
Capsule and other primitive meshes are in the Unity default resources and I know how to assign it in the editor but how do I obtain one in C#? Obviously instantiaion isn't available since it's a library asset.
UPDATE:
I would have thought that this would work:
meshFilter.mesh = Resources.Load<Mesh>("Capsule");
But the mesh is still null afterwards in the editor component inspector.

I am not aware about a direct way how to would not know a direct way how to do it, but there is the "hacky" way:
MeshFilter meshfilter = gameObject.AddComponent<MeshFilter>();
GameObject go = GameObject.CreatePrimitive(PrimitiveType.Capsule);
meshfilter.mesh = go.GetComponent<MeshFilter>().mesh;
meshfilter.gameObject.AddComponent<MeshRenderer>();
Destroy(go);
Does that help?

GameObject obj=GameObject.CreatePrimitive(PrimitiveType.Capsule);
obj.name="Player";

Related

Assign a Mesh to a Particle System using Script (Unity)

so I have a custom mesh that I have generated using script and I want to assign it in a particle system but nothing will work. The first part of my code is where I delcare a public Mesh in my script just so I can assign the mesh there when I generate it and then take it from there to assign it to the particle system.
public Mesh Mesh;
[...]
Mesh = generated_mesh;
[...]
var shape = ps.shape;
shape.enabled = true;
shape.shapeType = ParticleSystemShapeType.MeshRenderer;
shape.mesh = Mesh;
The type changed to Mesh Renderer but the Mesh itself doesn't change.
Okay so it seems I was doing it the wrong way all along. I changed the shape type from mesh renderer to mesh and it's all good now. The confusing part is that when I was testing the particle system the only way I could assign the mesh to it manually was through the shape called mesh renderer and not the simple mesh. Anyway, it's all good now.
var shape = ps.shape;
shape.enabled = true;
shape.shapeType = ParticleSystemShapeType.Mesh;
shape.mesh = Mesh;

Unity C# Adding Material Component to new GameObject

i have a problem about adding Material component to my new GameObject.
Situation: I have a big cube and a bullet. After bullet touches to the big cube. Big cube will be pieced 5x5x5. This is ok. I created small cubes for that. I wanna add to these cubes colors. I know that blabla.AddComponent();
blabla.GetComponent().xxx;
I used this method so as to add Rigidbody, but i couldn't add a Material.
My code:
piece.AddComponent();
piece.GetComponent().color = Color.red;
Can you say how can i add material ?
You can't, because Material is not a Component.
Rather, do the following:
var mr = GetComponent<MeshRenderer>();
mr.material = your material

How to get all the materials assigned to a GameObject in Unity

In Unity, we can get the material that the GameObject has with the following code.
Material myMaterial = GetComponent<Renderer>().material;
But with the above code, we can only get one material per GameObject.
However, in actually, Unity GameObjects can have more than one material.
As shown in the image below
There can be more than one material per GameObject if it is assigned on a face-by-face basis.
I tried to get multiple materials with the code below but it didn't work.
List<Material> myMaterials = GetComponent<Renderer>().material;
Is there a way to get multiple materials assigned to a GameObject?
You can use the Renderer.Materials: https://docs.unity3d.com/ScriptReference/Renderer-materials.html
List<Material> myMaterials = GetComponent<Renderer>().materials.ToList();

How can i add a renderer to gameobject in script?

GameObject cube = Cube.CreatePrimitive(Cube.CubePivotPoint.UPLEFT);
cube.GetComponent<Renderer>().material.color = Color.blue;
When i assign the color i'm getting exception on the line:
cube.GetComponent<Renderer>().material.color = Color.blue;
MissingComponentException: There is no 'Renderer' attached to the "CubeHolder" game object, but a script is trying to access it.
You probably need to add a Renderer to the game object "CubeHolder". Or your script needs to check if the component is attached before using it.
NOTE:
I am using the CUBE class from this answer to create a cube, not the Unity's GameObject.CreatePrimitive function.
When I wrote the CUBE class, I forgot to mention that the Cubes' renderer is now a child of another Object.
You don't need to add Renderer or MeshRenderer to the cube. It's is already there. The cube is simply a child object and the parent Object is named CubeHolder. You need to use GetComponentInChildren to get its Renderer.
cube.GetComponent<Renderer>().material.color = Color.blue;
should now be:
cube.GetComponentInChildren<Renderer>().material.color = Color.blue;

Detecting a single instance of a prefab

I am developing a unity project.In that I have a prefab where there will be multiple instances of that prefab will be generated in the scene.I want to select one instance of that prefab and change the color of that of that prefab.
void Update(){
if (Input.GetMouseButtonDown(1))
{
wallPrehab.renderer.material.color = Color.red;
}
}
but this changes all the instances of the prefab.How can I change it to a single object.
Change the color of the object by locating the instance of the prefab, not the prefab itself. Use a Raycast to locate the gameobject. (Since you are responding to mouse click event i assume that you are selecting the object which you want to change the color)
private RaycastHit hit;
void Update(){
Physics.Raycast (Camera.main.ScreenPointToRay (Input.mousePosition), out hit);
if (Input.GetMouseButtonDown(1) && hit.collider){
hit.collider.gameObject.renderer.material.color = Color.red;
}
}
Dont forgrt to add a Collider to the prefab.
Because all your objects use the same material it won't be possible to change ones color without affecting others , one way is to create a copy of that material and change that ones color , this way they aren't using the same material the only thing that you need to do differrent in your code is to get the reference to the instance not the prefab this way if you change the material at runtime a new material will be created
Gameobject go = Instantiate(myPrefab);
go.renderer.material.color = Color.blue;

Categories

Resources