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

Finding the renderer attatched to my sprite

Discussion in 'Scripting' started by Hakazaba, Dec 9, 2015.

  1. Hakazaba

    Hakazaba

    Joined:
    Jul 1, 2015
    Posts:
    119
    Hey, Im having a bit of a problem while trying to detect the renderer attached to my sprite. I'm getting this warning:

    MissingComponentException: There is no 'Renderer' attached to the "Image" game object, but a script is trying to access it.
    You probably need to add a Renderer to the game object "Image". Or your script needs to check if the component is attached before using it.
    HSVMatEdit.OnValidate () (at Assets/Scripts/HSVMatEdit.cs:19)
    UnityEditor.DockArea:OnGUI()


    And there is! There is dammit! Its right there! Canvas Renderer right under Rect Transform. What am i doing wrong?

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5. using HutongGames.PlayMaker;
    6.  
    7. [ExecuteInEditMode]
    8. public class HSVMatEdit : MonoBehaviour {
    9.  
    10.    FsmFloat hueVarGlobal = FsmVariables.GlobalVariables.FindFsmFloat ("playerUiHue");
    11.    FsmFloat satVarGlobal = FsmVariables.GlobalVariables.FindFsmFloat ("playerUiSat");
    12.    FsmFloat valVarGlobal = FsmVariables.GlobalVariables.FindFsmFloat ("playerUiVal");
    13.  
    14.    public float hueSetting;
    15.    public float saturationSetting;
    16.    public float brightnessSetting;
    17.    
    18.    void OnValidate() {
    19.      Renderer thisRenderer = this.gameObject.GetComponent<Renderer>();
    20.      thisRenderer.material.SetFloat ("_HueShift", hueSetting + hueVarGlobal.Value);
    21.      thisRenderer.material.SetFloat ("_Sat", saturationSetting + satVarGlobal.Value);
    22.      thisRenderer.material.SetFloat ("_Val", brightnessSetting + valVarGlobal.Value);
    23.    }
    24. }
     
  2. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    If you really really the Canvas Renderer:
    Code (csharp):
    1. CanvasRenderer thisRenderer = GetComponent<CanvasRenderer>();
    But, since you trying to access the material so tweak them, I think what you want is:
    Code (csharp):
    1. UI.Image thisImage = GetComponent<UI.Image>();
    2. thisImage.material.SetFloat( etc... )
    3.  
     
  3. Hakazaba

    Hakazaba

    Joined:
    Jul 1, 2015
    Posts:
    119
    Thanks for the reply, a couple of issues with that though. In the first example, the CanvasRenderer type doesnt seem to have a material.setfloat i can do.

    In the second, UI.Image doesnt appear to be something monodevelop approves of.
     
  4. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    Of course for the CanvasRenderer, that's why I spotted the second example
    You can check the documentation about CanvasRenderer to have a better knowledge about it.

    And for the second example, my bad, I didn't see that you already have imported the "using UnityEngine.UI;"
    Then, simply do:
    Code (csharp):
    1.  
    2. Image thisImage = GetComponent<Image>();
    3. thisImage.material.SetFloat( etc... )
    4.  
     
  5. Hakazaba

    Hakazaba

    Joined:
    Jul 1, 2015
    Posts:
    119
    YUS! It works! Thank you so much! So, this works by detecting the Image script rather than a renderer then? Weird that its kinda inconsistant.

    However! In other news. I found out that i can substitute .material for .GetMaterial() when working with CanvasRenderer. There doesn't seem to be a way to get shared materials though. Just something to know.
     
  6. Hakazaba

    Hakazaba

    Joined:
    Jul 1, 2015
    Posts:
    119
    Okay! Time has past, ive added more functionality to my script, and some new, OH MY GOD levels of errors that make my eyes bleed... So! Here goes. First, my script.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5. using HutongGames.PlayMaker;
    6.  
    7. [ExecuteInEditMode]
    8. public class HSVMatEdit : MonoBehaviour {
    9.  
    10.    public bool effectedByPlayerOptions;
    11.    public float hueSetting;
    12.    public float saturationSetting = 1;
    13.    public float brightnessSetting = 1;
    14.  
    15.    static int UIInstances;
    16.    //Image thisRenderer;
    17.    Material matInstance;
    18.  
    19.    void OnStart(){
    20.      if (Shader.Find ("HSVShader") == null){
    21.        Resources.Load("HSVShader")
    22.      }
    23.      if (matInstance == null) {
    24.  
    25.        matInstance = new Material (Shader.Find ("Shaders/HSVShader"));
    26.        matInstance.name = "uiMat_" + this.gameObject.name + UIInstances;
    27.        UIInstances++;
    28.        this.GetComponent<Image>().material = matInstance;
    29.      }
    30.    }
    31.    void OnValidate() {
    32.      //Image thisRenderer = GetComponent<Image> ();
    33.      if (effectedByPlayerOptions == false)
    34.      {
    35.        matInstance.SetFloat ("_HueShift", hueSetting*10);
    36.        matInstance.SetFloat ("_Sat", saturationSetting);
    37.        matInstance.SetFloat ("_Val", brightnessSetting);
    38.      }
    39.      else
    40.      {    
    41.        FsmFloat hueVarGlobal = FsmVariables.GlobalVariables.FindFsmFloat ("playerUiHue");
    42.        FsmFloat satVarGlobal = FsmVariables.GlobalVariables.FindFsmFloat ("playerUiSat");
    43.        FsmFloat valVarGlobal = FsmVariables.GlobalVariables.FindFsmFloat ("playerUiVal");
    44.        
    45.        matInstance.SetFloat ("_HueShift", hueSetting*10 + hueVarGlobal.Value);
    46.        matInstance.SetFloat ("_Sat", saturationSetting + satVarGlobal.Value);
    47.        matInstance.SetFloat ("_Val", brightnessSetting + valVarGlobal.Value);
    48.      }
    49.    }
    50. }
    51.  
    Now, the first error doesnt seem so scary, but i have no idea why it is there:

    Assets/Scripts/HSVMatEdit.cs(21,17): error CS1525: Unexpected symbol `}'

    Simple, but huh? Arent i supposed to have that symbol there for if statements?... Now for the second and much more horrifying one:


    NullReferenceException
    UnityEngine.Material..ctor (UnityEngine.Shader shader) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/ShaderBindings.gen.cs:168)
    HSVMatEdit.OnValidate () (at Assets/Scripts/HSVMatEdit.cs:21)
    System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)
    Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
    System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:232)
    System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115)
    UnityEngine.UI.Graphic.OnRebuildRequested () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Graphic.cs:456)
    UnityEngine.UI.GraphicRebuildTracker.OnRebuildRequested () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/GraphicRebuildTracker.cs:33)
    UnityEngine.CanvasRenderer.RequestRefresh () (at C:/buildslave/unity/build/artifacts/generated/common/modules/CanvasRendererBindings.gen.cs:332)

    As you can see, itspretty mind bogglingly large, but it seems to mainly be unable to find HSVShader... Which is in a rescources file. Can anyone help me with this one?
     
  7. troyfury

    troyfury

    Joined:
    May 15, 2015
    Posts:
    19
    For the first error Resources.Load("HSVShader") is missing a semi colon : Resources.Load("HSVShader");
     
  8. Hakazaba

    Hakazaba

    Joined:
    Jul 1, 2015
    Posts:
    119
    Ha! I cant believe i posted for such a simple mistake... I fixed the second by deleting and adding new components for testing. I have a new error now if you dont mind haveing a look at it:


    NullReferenceException: Object reference not set to an instance of an object
    HSVMatEdit.OnValidate () (at Assets/Scripts/HSVMatEdit.cs:34)
    UnityEditor.DockArea:OnGUI()
     
  9. Hakazaba

    Hakazaba

    Joined:
    Jul 1, 2015
    Posts:
    119
    Also this is still a problem:


    NullReferenceException
    UnityEngine.Material..ctor (UnityEngine.Shader shader) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/ShaderBindings.gen.cs:168)
    HSVMatEdit.OnValidate () (at Assets/Scripts/HSVMatEdit.cs:27)
     
  10. Hakazaba

    Hakazaba

    Joined:
    Jul 1, 2015
    Posts:
    119
    I managed to figure it all out this time on my own :D