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
Related
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();
I read many questions about this, but I still can't find what my problem is...
I'm trying to instantiate a prefab at the canvas. It is compose of a button and a sprite. The button looks ok, but the sprite is not visible at the Game (but is visible at the Scene).
I'm doing something wrong, but I can't see what...
[SerializeField] GameObject finishedLevel;
private void Start()
{
finishedLevel = Instantiate(finishedLevel, transform.position, transform.rotation);
finishedLevel.transform.SetParent(GameObject.FindGameObjectWithTag("Canvas").transform, false);
}
SpriteRenderer is not made to be used with the Canvas. You are confusing and misusing the two.
SpriteRenderer is used for displaying 2D Objects like a 2D animated character or a 2D environment. You can attach Rigidbody2D and Colliders to SpriteRenderer.
Canvas is used for UI display only. It is used for displaying things such as UI texts, buttons, sliders, scrollbars and images. You shouldn't attach Rigidbody2D and Colliders to it or its child objects.
With the explanation above, you should be able to determine which one to use. If you just need to display image under a Canvas, use the Image, or RawImage component since they are part of the UI system. This should work but do not make SpriteRenderer a child of a Canvas. If you have to use SpriteRenderer, make it its own object or under another object but it should not be under a Canvas. You may find Unity's UI tutorial useful.
I´m making a simple CAD application on Unity 3D using C# to configure closets. I'm trying to limit the movement of the shelves so you can move them only on the hole, so I have a dragger attached to every piece of the closet I want to move, but obviously they can cross with each other.
I thought I could use the collider system that Unity has to limit this movement but since I never worked with Unity before I´m kinda lost. This is my dragger so far:
mousePosition = new Vector3(Input.mousePosition.x, cubo_tmp.transform.position.y, distance);
objPosition = Camera.main.ScreenToWorldPoint(mousePosition);
objPosition.Set(objPosition.x, cubo_tmp.transform.position.y, cubo_tmp.transform.position.z);
I keep y and z components so it can move only in one direction.
Box Colliders are best for cuboid shaped objects.
In the editor, if you click GameObject > 3d Object > Cube, Unity will add a cube with a Box Collider to your scene:
If you want to add a Box Collider to a GameObject that doesn't have one, click Add Component in the Inspector panel and type in "Box Collider" to find it. It looks like this:
Note: If the IsTrigger box is checked, the Collider acts as a Trigger rather than a Collider.
A GameObject with a Collider will "collide" with any other GameObject that also has a Collider, with a small exception involving 2 Mesh Colliders that are both set to Convex.
EDIT: I think you are able to intersect the objects because you are modifying Transform.position directly in your code. This might override the collision behaviour.
Also Go to Edit > Project Settings > Physics and make sure your layer collision matrix allows collisions for the layers on which your objects are placed.
I am working on a project in my game programming class, and I want to change the friction of a physics2D material, and I can't find anything about changing it. Do you need to import anything, and what would the code be? thanks!
There are a couple ways you can do this.
From the UI
From the code (in C#)
gameObject.GetComponent(CircleCollider2D).sharedMaterial.friction = 0.0f
Where gameObject is your gameObject
Refs
Physics Material 2d
Box Collider 2d
Shared Material
sorry for my bad english. I'm from germany^^
My question:
Is it possible to transform an empty game object into a shape that Looks
like a Piece of cake? I have a round terrain and I want to "cut" it in
three pieces of cake, because I want to play different music in all three areas. I Need These pieces of cake as a box/mesh collider or something like this, so I can Play different Songs if the Player collides with the box/mesh collider.
Ideas anyone? Please help me. :)
Is it possible to build an object in Blender for example and use it
as an invisible box collider?
Create the shape in Blender. Create a MESH COLLIDER. Assign your piece of cake model to the MESH parameter of the MESH collider. Scale it. Position it. set it as trigger and VOILA!
This would be the mesh you create in blender (RED)
then,
void OnTriggerEnter(Collider other) {
//play song
}
The other answers have covered options of generating a mesh outside of Unity and using it to make a mesh collider trigger for your music changes.
Other options:
Native Unity collider option:
Use two native cube mesh colliders to create a sandwich of two thin colliders at the borders where the music needs to change. Do some OnTriggerEnter/OnTriggerExit scripting to make the music change correctly when the player crosses the borders.
Pure scripting option:
Use the player position relative to the "center of the cake" point to calculate when to change the music based on where the player is located.