Search Unity

error CS0165: Use of a unassigned variable 'temp'

Discussion in 'Scripting' started by Zarto, Jan 28, 2015.

  1. Zarto

    Zarto

    Joined:
    Jan 28, 2015
    Posts:
    1
    I am wondering if anyone could help me with this error my C# code is:

    using UnityEngine;
    using System.Collections;

    public class GameTime : MonoBehaviour {
    public enum TimeOfDay {
    Idle,
    SunRise,
    SunSet

    }
    public Transform[] sun;
    public float dayCycleInMinutes = 1;

    private const float SECOND = 1;
    private const float MINUTE = 60 * SECOND;
    private const float HOUR = 60 * MINUTE;
    private const float DAY = 60 * MINUTE;

    public float sunRise;
    public float sunSet;
    public float skyboxBlendModifier;

    private float _dayCycleInSeconds;

    private const float DEGREES_PER_SECOND = 360 / DAY;

    private float _degreeRotation;

    private float _timeOfDay;

    private TimeOfDay _tod;



    //Usethisfor initialization
    void Start () {
    _tod = TimeOfDay.Idle;


    _dayCycleInSeconds = dayCycleInMinutes * MINUTE;



    RenderSettings.skybox.SetFloat("_Blend",0);

    _timeOfDay = 0;
    _degreeRotation = DEGREES_PER_SECOND * DAY / (_dayCycleInSeconds);

    sunRise *= _dayCycleInSeconds;
    sunSet *= _dayCycleInSeconds;

    }

    //Updateiscalledonceper frame
    void Update () {
    for(int cnt = 0; cnt < sun.Length; cnt++)
    sun[cnt].Rotate(new Vector3(_degreeRotation, 0, 0) * Time.deltaTime);

    BlendSkybox ();

    if (_timeOfDay > _dayCycleInSeconds)
    _timeOfDay -= _dayCycleInSeconds;


    _timeOfDay += Time.deltaTime;
    //Debug.Log(_timeOfDay);

    if (_timeOfDay > sunRise) {
    _tod = GameTime.TimeOfDay.SunRise;
    BlendSkybox ();

    }
    }
    private void BlendSkybox() {
    float temp;

    switch(_tod) {
    case TimeOfDay.SunRise:
    temp = (_timeOfDay - sunRise)/ _dayCycleInSeconds * skyboxBlendModifier;
    break;
    case TimeOfDay.SunSet:
    temp = (_timeOfDay - sunSet) / _dayCycleInSeconds * skyboxBlendModifier;
    temp = 1 - temp;
    break;

    }

    RenderSettings.skybox.SetFloat("_Blend", temp);

    Debug.Log(temp);

    }

    }

    Thanks.
     
  2. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    If _tod gits the "default" case, temp will still be Null! Therefore the compilersays uh oh, a parameter RenderSettings.skybox.SetFloat("_Blend", temp) is not set!

    Try float temp = 0; ;)
     
  3. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137