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 :: print in c# unity 
Csharp :: how to draw gizmos unity 
Csharp :: c# read char 
Csharp :: c# split string for all blank character 
Csharp :: unity destroy all children 
Csharp :: unity c# delay function 
Csharp :: create models from database ef core 
Csharp :: c# throw exception 
Csharp :: c# return list 
Csharp :: C# datetime.now to string only numbers 
Csharp :: c# mongodb connection 
Csharp :: string length c# 
Csharp :: how consider the first caracter in Split c# 
Csharp :: best way to compare byte array c# 
Csharp :: c# postmessage mouse click 
Csharp :: define a vector c# 
Csharp :: words counter c# 
Csharp :: unity custom update 
Csharp :: dynamic convert type c# 
Csharp :: how to print a matrix in c# 
Csharp :: c# how to refreshyour bindingsource 
Csharp :: get folders in directory c# 
Csharp :: get 2d rotation from 2 position math 
Csharp :: button size xamarin 
Csharp :: crop bitmap image c# 
Csharp :: topdown unity 
Csharp :: unity find gameobject 
Csharp :: c# type from string 
Csharp :: solidity get address of contract 
Csharp :: add item to list c# 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =