Another "Object reference not set to an instance of an object"

Hey guys!!! I’ve got a quick problem here: so i have this property from a custom shader that needs to lerp out but instead it gives me this error in the thread title on line 41 PlayerShield script and the “Fresnel Scale” (which is the property that im trying to change) does not even change to 0.2f from 1f (default value) however in the Start funciton everything works fine whithout problem.

In Start funciton if I were to uncomment that last line it would work so the problem is only in the Update()

Code:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class PlayerShield : MonoBehaviour
{
    public int startingHealth = 100;                          
    public int currentHealth;                                                                                                                               
    public GameObject explsoion;
    public PlayerMovement playerMovement;

    Renderer rend;
    Material material;                               
    bool isDead ;                                            
    bool damaged;


    void Awake ()
    {
        // Setting up the references.
        //anim = GetComponent <Animator> ();
        //playerAudio = GetComponent <AudioSource> ();
        playerMovement = GetComponent <PlayerMovement> ();
       
        currentHealth = startingHealth;
    }
    void Start ()
    {
        Renderer rend = GetComponent<Renderer>();
        rend.material.shader = Shader.Find ("FresnelPack/Transparent Rim Unlit");
        //rend.material.SetFloat ("_Scale", 0.0f);
    }
   
    void Update ()
    {
        this.rend.material.SetFloat ("_Scale", Mathf.Lerp (0.2f, 0f, Time.time * 1));
    }

The local rend in Start() overrides the global rend.
Change line 29 to:

rend = GetComponent<Renderer>();
1 Like

I’m not sure if you’re meaning to do it… but you have two version of the variable ‘rend’.

One at the top as a class variable, and one you’re creating in the Start function (which’ll only be accesssible in the Start function).

What you may be wanting to do is instead

void Start ()
    {
        rend = GetComponent<Renderer>();
        rend.material.shader = Shader.Find ("FresnelPack/Transparent Rim Unlit");
        //rend.material.SetFloat ("_Scale", 0.0f);
    }

have it like that :).

omg thanks man didn’t notice that. hahahaah