Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

unity change material

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DeerScript : MonoBehaviour
{
	// this is gameobject whose material will be changed
    public GameObject target;
    
    // this is array of materials
    // which is assigned in inspector
    public Material[] materials;
    
    // in this example i have used trigger
    private void OnTriggerEnter(Collider other)
    {
    	// this the type of mesh renderin used in gameobject
        // your may be different  like MeshRenderer
        // you can find in inspector of target
        target.GetComponent<SkinnedMeshRenderer>().materials = materials;
    }
}
// here is demo of that example but it slightly differs from the above 
// instead of array pre assigned is have used aonther way around
// demo is in the link
// if this help please subscribe my youtube channel
// https://youtu.be/XY6XiLEh4II
Comment

unity change material

var newObstacle = Instantiate(cube, new Vector3(i, 0, j), Quaternion.identity, transform);
Renderer obstacleRenderer = newObstacle.GetComponent<Renderer>();

Material mat = new Material(obstacleRenderer.sharedMaterial);
mat.color = GetRandomColor();
obstacleRenderer.sharedMaterial = mat;
// for reference methods
public Color GetRandomColor()
{
  return new Color(Random.Range(0, 255),Random.Range(0, 255),Random.Range(0, 255));
}

// for specific
public Color GetRandomColor()
{
    var random = Random.Range(1, 10); 
    if(random<4) return Color.red; 
    else if(random<7) return Color.blue; 
    else return Color.green; 
}
Comment

change object material unity

Material[] materials = meshRenderer.materials;
Material oldMaterial = materials[0];
Material[] newMaterials = new Material[] { SphereMaterial};
meshRenderer.materials = newMaterials;
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# write line variable 
Csharp :: c# use enum in class 
Csharp :: csharp nullable types 
Csharp :: populate combobox from array c# 
Csharp :: make sprite invisible unity 
Csharp :: millie bobby brown age 
Csharp :: Selecting item from listview in C# 
Csharp :: get multi-selected rows gridcontrol devexpress 
Csharp :: MailChimp C# Api calls 
Csharp :: width="331" height="331" 
Csharp :: How to use multiple Commands for one ViewModel 
Csharp :: how to do Employing defensive code in the UI to ensure that the current frame is the most top level window in c# 
Csharp :: laravel get current url 
Html :: open markdown link in new tab 
Html :: espacio html 
Html :: regex href html pattern 
Html :: http://127.0.0.1:5500/favicon.ico 
Html :: divi font awesome 
Html :: whatsapp html code for website 
Html :: target blanc 
Html :: html disable first option 
Html :: Making a Phone number a link 
Html :: bootstrap modal fullscreen 
Html :: twitter icon html5 
Html :: mobile prevent zoom 
Html :: html pi 
Html :: href rel 
Html :: meta description 
Html :: img tamanho html 
Html :: video tag html 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =