Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Problems accessing shader parameter from within another script

Discussion in 'Scripting' started by AlmantusK, Oct 13, 2015.

  1. AlmantusK

    AlmantusK

    Joined:
    Oct 7, 2015
    Posts:
    30
    Hello, I have a little problem here: each time I try to setFloat to my shader parameter, it gives me this error
    "UnassignedReferenceException: The variable rn of Glowinteract has not been assigned.
    You probably need to assign the rn variable of the Glowinteract script in the inspector.
    Glowinteract.Update () (at Assets/Scripts/Glowinteract.cs:26)". But the strangest thing is that getFloat method works. So I am wondering what is wrong?
    Here's my script that sets the value:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Glowinteract : MonoBehaviour {
    5.  
    6.     private float power;
    7.     private Renderer rn;
    8.     private bool forward;
    9.     void Start () {
    10.         Renderer rn = gameObject.GetComponent<Renderer> ();
    11.         power = rn.material.GetFloat("_RimPower");
    12.         print(rn.material.GetFloat("_RimPower"));
    13.     }
    14.  
    15.     void Update () {
    16.         if (power >= 6.0f)
    17.                 forward = false;
    18.         if (power >= 1.0f && power < 6.0f)
    19.                 forward = true;
    20.         if (forward)
    21.             power = power +Time.deltaTime;
    22.         else
    23.             power = power -Time.deltaTime;
    24.       rn.material.SetFloat("_RimPower", power);
    25.     }
    26. }
    27.  
     
  2. AlmantusK

    AlmantusK

    Joined:
    Oct 7, 2015
    Posts:
    30
    I realised that if I put SetFloat() inside Start(), it works, but I want it to be more dynamic, no just one defined value. So why would the update function no longer recognize my renderer?
     
  3. AlmantusK

    AlmantusK

    Joined:
    Oct 7, 2015
    Posts:
    30
    OKay, solved the issue.. No more programming at 2am. It was gameOject.getComponent<Renederer>().material.SetFloat() that I had to use.. Sorry for trouble.