script to replace material on object with timer or button press not working
Hi,
I'm trying to do a simple blinking effect by replacing the texture/material on a character mesh with a blinking or non-blinking version of the character texture. I've looked up a couple of tutorials and explanations for changing materials and combined this with both a timer and a button press triggered material change but neither seems to work.
This is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class anim : MonoBehaviour {
public Material[] material;
Renderer rend;
float timeLeft = 60.0f;
// Use this for initialization
void Start ()
{
rend = GetComponent<Renderer>();
rend.enabled = true;
rend.sharedMaterial = material[0];
//var timeLeft = 60;
//gameObject.GetComponent<Animation>().Play();
}
// Update is called once per frame
void Update () {
timeLeft -= Time.deltaTime;
if (timeLeft < 30)
{
rend.sharedMaterial = material[1];
//timeLeft = 30;
}
if (timeLeft < 0)
{
rend.sharedMaterial = material[0];
timeLeft = 60.0f;
}
if(Input.GetKeyDown (KeyCode.Space))
{
rend.sharedMaterial = material[1];
}
if (Input.GetKeyDown(KeyCode.Return))
{
rend.sharedMaterial = material[0];
}
}
}
The Material array with slots 0 and 1 appears as a component on the character object and I've assigned the two materials to a slot each. But when I run the game and wait for the timer or press SPACE or RETURN, nothing happens.
The character is a fully animated mesh with bones imported from FBX if that makes any difference.
I should note that I am very much unfamiliar with coding in Unity so I've probably missed something obvious. Any help would be greatly appreciated.
Try removing all code in Update
except:
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space)) {
rend.shared$$anonymous$$aterial = material[1];
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Return)) {
rend.shared$$anonymous$$aterial = material[0];
}
Then please let me know if the material switches when you hit Space or Return. Thanks.
If I remove the timer code from Update it still does nothing when I press SPACE or RETURN.
Can you post a photo of your current inspector setup on the script? (The entire inspector, including the attached renderer.) Thanks!
Side note, with the timer code left in it feels like it should really be if (timeLeft = 30) ins$$anonymous$$d of < 30 but when I do that it says "Cannot implicitly convert type 'float' to 'bool'". No idea what to do about that.
Answer by ninjadodo · Nov 24, 2017 at 12:19 PM
Here's the inspector with the character object and the script.
(unrelated side note: attaching images to posts on this forum is confusing)
I should note also I've tested the "Astromat_blink" material by drag-and-dropping it onto the object by hand and that works (changing the texture as expected), so the material itself does not seem to be the problem.
Answer by NinjaISV · Nov 24, 2017 at 12:57 PM
The problem is that you are using GetComponent
to assign your Renderer. Just make the field rend
public, then assign the renderer of the object to the inspector and it will work. NOTE: You must remove the code that reassigns the Renderer by getting the component. Your class should look like:
using UnityEngine;
public class anim : MonoBehaviour {
public Material[] material;
public Renderer rend;
float timeLeft = 60.0f;
// Use this for initialization
private void Start () {
rend.enabled = true;
rend.sharedMaterial = material[0];
}
// Update is called once per frame
private void Update () {
if (Input.GetKeyDown (KeyCode.Space)) {
rend.sharedMaterial = material[1];
}
if (Input.GetKeyDown(KeyCode.Return)) {
rend.sharedMaterial = material[0];
}
}
}
If I copy/paste that code, replacing the original script, it still doesn't respond to SPACE when I run the game, leaving the material at the starting default.