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. Dismiss Notice

How to initialize local variable instant from other class?

Discussion in 'Scripting' started by Ultimate360, Jul 22, 2017.

  1. Ultimate360

    Ultimate360

    Joined:
    Oct 28, 2016
    Posts:
    27
    Hi, I really don't know how to initialize the value of local variable instant from other class. Below I made simple testing script; this will help me in my real script working on. Please help me understand and what to do...

    This script will work, of course:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class varElements {
    6.     public bool changeVar = true;
    7.     public int varInt;
    8.     public float varFloat;
    9.     public string varString;
    10.     public enum varEnumList {
    11.         enumList1,
    12.         enumList2,
    13.         enumList3,
    14.         enumList4
    15.     }; public varEnumList varEnumType;
    16. }
    17.  
    18. public class InitializeInstanceClassVar : MonoBehaviour {
    19.  
    20.     public varElements editorSetup;
    21.  
    22.     // Use this for initialization
    23.     void Start () {
    24.         if (editorSetup.changeVar) {
    25.             editorSetup.varInt = 100;
    26.             editorSetup.varFloat = 40.5f;
    27.             editorSetup.varString = "Sucessful";
    28.             editorSetup.varEnumType = varElements.varEnumList.enumList2;
    29.         }
    30.         Debug.Log ("editorSetup.varString: " + editorSetup.varString);
    31.     }
    32. }
    This is where I'm confuse or don't understand, sorry being a noob... This will give me error: CS0165: Use of unassigned local variable `tempLocalVar'; I really don't understand why invalid? As I only want to make temporary variable with initialization;
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class varElements {
    6.     public bool changeVar = true;
    7.     public int varInt;
    8.     public float varFloat;
    9.     public string varString;
    10.     public enum varEnumList {
    11.         enumList1,
    12.         enumList2,
    13.         enumList3,
    14.         enumList4
    15.     }; public varEnumList varEnumType;
    16. }
    17.  
    18. public class InitializeInstanceClassVar : MonoBehaviour {
    19.  
    20.     public varElements editorSetup;
    21.  
    22.     // Use this for initialization
    23.     void Start () {
    24.         if (editorSetup.changeVar) {
    25.             editorSetup.varInt = 100;
    26.             editorSetup.varFloat = 40.5f;
    27.             editorSetup.varString = "Sucessful";
    28.             editorSetup.varEnumType = varElements.varEnumList.enumList2;
    29.         }
    30.         Debug.Log ("editorSetup.varString: " + editorSetup.varString);
    31.         localFunctionOrMethod ();
    32.     }
    33.  
    34.     //Error: error CS0165: Use of unassigned local variable `tempLocalVar'
    35.     void localFunctionOrMethod () {
    36.         varElements tempLocalVar;
    37.  
    38.         tempLocalVar.varInt = 100;
    39.         tempLocalVar.varFloat = 40.5f;
    40.         tempLocalVar.varString = "Sucessful";
    41.         tempLocalVar.varEnumType = varElements.varEnumList.enumList2;
    42.  
    43.         Debug.Log ("tempLocalVar.varString: " + tempLocalVar.varString);
    44.     }
    45. }
    Also, why:
    Code (CSharp):
    1. editorSetup.varEnumType = varElements.varEnumList.enumList2;
    Can't be:
    Code (CSharp):
    1. editorSetup.varEnumType = editorSetup.varEnumList.enumList2;
    ^ Error: error CS0572: `varEnumList': cannot reference a type through an expression; try `varElements.varEnumList' instead

    Why this happening? Thank you so much for the help...
     
    Last edited: Jul 22, 2017
  2. Zephus

    Zephus

    Joined:
    May 25, 2015
    Posts:
    356
    First of all - it's confusing as hell to read those generic names for everything and debug properly. You also should follow basic naming conventions. Class names should be written in capital letters for example. It's just confusing.

    Regarding your questions:
    1. After declaring tmpLocalVar you need to use its constructor:
    Code (CSharp):
    1. varElements tempLocalVar = new varElements();
    2. enum types are part of the class, so you reference that and not the actual instance.
     
    Ultimate360 likes this.
  3. Ultimate360

    Ultimate360

    Joined:
    Oct 28, 2016
    Posts:
    27
    @Zephus thank you... sorry if I forgot to make the class name and function name start with capital letter, in the actual code I do not do that, except I just forgot with this dummy script. Sorry if I made my sample code generic, I thought it will easier to understand if I do that than actual code... anyway, thank for the help. Appreciated it...
     
  4. Ultimate360

    Ultimate360

    Joined:
    Oct 28, 2016
    Posts:
    27
    Ah, follow up question (the last one): is it a good idea/practice if I also put = new varElements() to line 20, even it will work without it? That is:
    Code (CSharp):
    1. public varElements editorSetup = new varElements();
    Thanks!
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,195
    What on earth is your code supposed to be doing? Is it some kind of generalized storage of different data types?


    Untiy sets public fields to whatever value you set them to last. For new objects, the default value will be used. If you decalre the field with a value, that'll be used instead of the default.

    In your case, the default value for varElements is what you get from calling the default constructor, so there's no point. You could set a different default value if you wanted. Here's an example script showing how it works.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. //Put me on a gameobject!
    5. public class DefaultValue : MonoBehaviour {
    6.    public Test t1;
    7.    public Test t2 = new Test { i = 2, f = 2f, s = "2" };
    8.    public Test t3 = Test.CreateTest();
    9.    public Test t4 = new Test(4, 4, "4");
    10. }
    11.  
    12. [System.Serializable]
    13. public struct Test {
    14.    public int i;
    15.    public float f;
    16.    public string s;
    17.  
    18.    public Test(int i, float f, string s) {
    19.       this.i = i;
    20.       this.f = f;
    21.       this.s = s;
    22.    }
    23.  
    24.    public static Test CreateTest() {
    25.       return new Test { i = 3, f = 3f, s = "3" };
    26.    }
    27. }
     
    Ultimate360 and Ironmax like this.