- Home /
Change material of a single prefab
I want to change the material of a prefab every time damage is equal 2, however this changes every single instance of the prefab.
This the script in the prefab
public Material[] materials;
public Renderer rend;
void start()
{
rend = GetComponent<Renderer>();
rend.enabled = true;
damage = 1;
}
void Update()
{
if (damage == 2)
{
rend.material = materials[0];
}
}
this one is the player's script that instantiate the prefabs.
public Rigidbody ball;
private void Shotter()
{
if (Input.GetMouseButton(0) && ballSpeed< 50.0f)
{
ballSpeed += 25.0f * Time.deltaTime;
//Debug.Log(ballSpeed);
filler.fillAmount += 0.5f * Time.deltaTime;
}
if (Input.GetMouseButtonUp(0) && ballNumber < ballNumberLimit)
{
Rigidbody ballInstance;
ballInstance = Instantiate(ball, barrelEnd.position, barrelEnd.rotation) as Rigidbody;
cubes.Add(ballInstance);
ballInstance.velocity = barrelEnd.forward * ballSpeed;
ballSpeed = 0.0f;
filler.fillAmount = 0.0f;
ballNumber += 1;
}
}
Prefabs are no longer Prefabs at runtime. I don't see any problem in your code. Are you sure that the damage value is different for every instance?
yeah that was it, I was using a static variable for the damage. I'm an idiot.
Love you!
Answer by SamElTerrible · Jan 02, 2017 at 04:54 PM
I have a similar code and it works fine for me. Maybe try setting the prefab's renderer to private? There is no need to expose it if you're finding it through GetComponent and it might be the cause of your issue.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Changing the material on a prefab 0 Answers
How to effect and apply a common material to children individually... 2 Answers
Tower defense target update 0 Answers