0

basicaly I want to get float variable from script SimpleLUT to WriteJson. All done in Unity 5.6 and Visual Studio 2015. Can someone please explain to me what am I doing wrong?

SimpleLUT.cs

    using System;
    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;

    namespace DigitalRuby.SimpleLUT
    {
        [ExecuteInEditMode]
        public class SimpleLUT : MonoBehaviour
        {

            [Range(0, 360)]
            [Tooltip("Hue")]
            public float Hue = 0.0f;

    //rest of the code

WriteJson.cs

using System.Collections;
using System.IO;
using UnityEngine;
using LitJson;
using DigitalRuby.SimpleLUT;

public class WriteJson : MonoBehaviour {

    public SimpleLUT script;

    // Use this for initialization
    void Start () {
        GameObject camera = GameObject.Find("Camera");
        SimpleLUT simpleLUT = camera.GetComponent<SimpleLUT>();

    }

public class ColorCorrectionSettings
    {
        public float JsonHue;
        public float JsonAmount;
        public float JsonSaturation;
        public float JsonBrightness;
        public float JsonContrast;

        public ColorCorrectionSettings(float JsonHue, float JsonAmount, float JsonSaturation, float JsonBrightness, float JsonContrast)
        {
            SimpleLUT.Hue = JsonHue; //<- here is the error
        }
    }

}

The SimpleLUT.Hue does not work. As it is non static, other static variables are accessable.

Programmer
  • 121,791
  • 22
  • 236
  • 328
Moss
  • 37
  • 6
  • Why not just pass a reference of `SimpleLUT` into the constructor for `ColorCorrectionSettings`? – Sean May 10 '17 at 18:56
  • What makes you think it should work? `ColorCorrectionSettings` does not have a property called `SimpleLUT` and the `Hue` property is not static – maccettura May 10 '17 at 18:57
  • I am newbie here. Would you care to explain or link me to some decent help? – Moss May 10 '17 at 18:59
  • I would recommend picking up a book on the basics/fundamentals of c#. If you just want guidance on your specific problem, look into the difference between static members and instance members. – maccettura May 10 '17 at 19:09
  • Your error here is a typo. Change `SimpleLUT.Hue` to `simpleLUT.Hue` (changes the reference from the static *Class* reference to the *instance* reference) – Draco18s no longer trusts SE May 10 '17 at 20:37

1 Answers1

-1

This is your SimpleLUT script:

public class SimpleLUT : MonoBehaviour
{
    [Range(0, 360)]
    [Tooltip("Hue")]
    public float Hue = 0.0f;
}

You can't access the Hue variable directly with SimpleLUT.Hue = JsonHue; because the Hue variable is not a static variable.

You have two optiopns:

1.Make the Hue variable static.

So, public float Hue = 0.0f; should be public static float Hue = 0.0f;.

I am sure you don't want this because it won't show up in the Editor when you make it static.

2.Create instance of SimpleLUT or get the instance.

Find the GameOjbect the SimpleLUT is attached to:

GameObject obj = GameObject.Find("GameOjbectSimpleLUTScriptIsAttachedTo");

Get the SimpleLUT script instance that is attached to that GameObject:

SimpleLUT simpleLUTInstance = obj.GetComponent<SimpleLUT>();

Now, you can access the Hue variable:

simpleLUTInstance.Hue = JsonHue;

Finally, this what it should look like:

public ColorCorrectionSettings(float JsonHue, float JsonAmount, float JsonSaturation, float JsonBrightness, float JsonContrast)
{
    GameObject obj = GameObject.Find("GameOjbectSimpleLUTScriptIsAttachedTo");
    SimpleLUT simpleLUTInstance = obj.GetComponent<SimpleLUT>();
    simpleLUTInstance .Hue = JsonHue; //<- No more error
}

Don't forget to replace "GameOjbectSimpleLUTScriptIsAttachedTo" with the name of GameObject the SimpleLUT script is attached to.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Aha! So the instance should be right before the linking. Thank you for the final look. – Moss May 10 '17 at 19:11
  • If `SimpleLUT` is attached to a GameObject then the instance exist on that GameObject. If it is not attached to any GameObject, you can create new instance with `AddComponent` since `SimpleLUT` inherits from `MonoBehaviour`. See [here](http://stackoverflow.com/questions/37398538/unity-null-while-making-new-class-instance/37399263#37399263) for more information. – Programmer May 10 '17 at 19:14