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

Re-Assigning Variable After Already Used

Discussion in 'Scripting' started by EternalAmbiguity, Oct 24, 2015.

  1. EternalAmbiguity

    EternalAmbiguity

    Joined:
    Dec 27, 2014
    Posts:
    3,144
    Alright, so I begin what I'm doing here by instantiating a UI panel the same number of times as a user's input to a field. I also name them based on that number: the first instance is called NMP1.1, the second NMP1.2, and so on.

    Then, to move between them (with a simple button), I first assign a float to a base value of 1.0, then make a new variable, searching for the GameObject with the name of my current page + 0.1, which works successfully. Then in the same function or whatever at the end I increment my float by 0.1. This works as well (by setting the float public, I can pause the scene after it's playing and see the number update in the inspector).

    The idea behind all that is for the float to update to a new value relating to the next instantiated panel, which can then be called by the same variable used before.

    But something doesn't appear to be working correctly. It works for the first instance, but after that it gives me "Object reference not set to instance of object." I think that AFTER I first assign the variable, when I try to reassign it it isn't working. Does anyone have any idea how I might fix this?

    Here's my code, a few unnecessary things omitted for space.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. public class Buttons : MonoBehaviour {
    7.     CanvasGroup[] panels;
    8.     public GameObject mmp;
    9.     public GameObject nmp;
    10.     public Dropdown orderselect;
    11.     public Text numbercelltype;
    12.     public GameObject nmpcell;
    13.     GameObject nmpnow;
    14.     public float pageCounter = 1.0f;
    15.  
    16.     // Use this for initialization
    17.     void Start ()
    18.     {
    19.         panels = GameObject.Find("Canvas").GetComponentsInChildren<CanvasGroup>();
    20.         foreach (CanvasGroup panel in panels)
    21.         {
    22.             panel.alpha = 0;
    23.             panel.interactable = false;
    24.             panel.blocksRaycasts = false;
    25.         }
    26.         mmp = GameObject.FindGameObjectWithTag("MainMenuPanel");
    27.         mmp.GetComponent<CanvasGroup>().alpha = 1;
    28.         mmp.GetComponent<CanvasGroup>().interactable = true;
    29.         mmp.GetComponent<CanvasGroup>().blocksRaycasts = true;
    30.     }
    31.    
    32.     // Update is called once per frame
    33.     void Update ()
    34.     {
    35.    
    36.     }
    37.  
    38.     public void StartNewModel()
    39.     {
    40.         ...
    41.     }
    42.  
    43.     public void ApplyNumber()
    44.     {
    45.         GameObject nmpcellclone;
    46.         //int pageNumber = 0;
    47.         nmpcell = GameObject.FindGameObjectWithTag("NMPCell");
    48.         numbercelltype = GameObject.FindGameObjectWithTag("numbercelltypefirstorder").GetComponent<Text>();
    49.         for (int pageNumber = 1; pageNumber < (int.Parse(numbercelltype.text) + 1); pageNumber ++)
    50.         {
    51.         nmpcellclone = Instantiate(nmpcell);
    52.        
    53.         nmpcellclone.transform.SetParent(GameObject.Find("Canvas").transform, false);
    54.         nmpcellclone.name = ("NMP" + "1." + pageNumber);
    55.         }
    56.     }
    57.  
    58.     public void Back1()
    59.     {
    60.        ...
    61.     }
    62.     public void Next1()
    63.     {
    64.         orderselect = GameObject.FindGameObjectWithTag("OrderDropDown").GetComponent<Dropdown>();
    65.         nmpnow = GameObject.Find("NMP" + (pageCounter + 0.1));
    66.         panels = GameObject.Find("Canvas").GetComponentsInChildren<CanvasGroup>();
    67.         foreach (CanvasGroup panel in panels)
    68.         {
    69.             panel.alpha = 0;
    70.             panel.interactable = false;
    71.             panel.blocksRaycasts = false;
    72.         }
    73.        
    74.  
    75.         {
    76.             nmpnow.GetComponent<CanvasGroup>().alpha = 1;
    77.             nmpnow.GetComponent<CanvasGroup>().interactable = true;
    78.             nmpnow.GetComponent<CanvasGroup>().blocksRaycasts = true;
    79.             pageCounter += 0.1f;
    80.         }
    81.     }
    82. }
    83.  
     
  2. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    I've not reviewed you code fully, but it's probably much easier and faster to store all of the created object in a array instead of using GameObject.Find.
     
  3. EternalAmbiguity

    EternalAmbiguity

    Joined:
    Dec 27, 2014
    Posts:
    3,144
    Thanks, I'm still a newbie at coding.

    Any comment on the variable assigning?
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    errors have line numbers, what line is your error complaining about (you'll have to tell us which line it is in the code you've pasted given that you've snips sections out of it... )
     
  5. EternalAmbiguity

    EternalAmbiguity

    Joined:
    Dec 27, 2014
    Posts:
    3,144
    Sorry, I'll post the full code here so there isn't that problem.

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class Buttons : MonoBehaviour {
    6.     CanvasGroup[] panels;
    7.     public GameObject mmp;
    8.     public GameObject nmp;
    9.     public Dropdown orderselect;
    10.     public Text numbercelltype;
    11.     public GameObject nmpcell;
    12.     public GameObject probinputfield;
    13.     public Text probvalue;
    14.     public Text probtitle;
    15.     GameObject nmpnow;
    16.     public float pageCounter = 1.0f;
    17.  
    18.     // Use this for initialization
    19.     void Start ()
    20.     {
    21.         panels = GameObject.Find("Canvas").GetComponentsInChildren<CanvasGroup>();
    22.         foreach (CanvasGroup panel in panels)
    23.         {
    24.             panel.alpha = 0;
    25.             panel.interactable = false;
    26.             panel.blocksRaycasts = false;
    27.         }
    28.         mmp = GameObject.FindGameObjectWithTag("MainMenuPanel");
    29.         mmp.GetComponent<CanvasGroup>().alpha = 1;
    30.         mmp.GetComponent<CanvasGroup>().interactable = true;
    31.         mmp.GetComponent<CanvasGroup>().blocksRaycasts = true;
    32.     }
    33.  
    34.     // Update is called once per frame
    35.     void Update ()
    36.     {
    37.  
    38.     }
    39.  
    40.     public void StartNewModel()
    41.     {
    42.         panels = GameObject.Find("Canvas").GetComponentsInChildren<CanvasGroup>();
    43.         foreach (CanvasGroup panel in panels)
    44.         {
    45.             panel.alpha = 0;
    46.             panel.interactable = false;
    47.             panel.blocksRaycasts = false;
    48.         }
    49.         nmp = GameObject.FindGameObjectWithTag("NewModelPanel");
    50.         nmp.GetComponent<CanvasGroup>().alpha = 1;
    51.         nmp.GetComponent<CanvasGroup>().interactable = true;
    52.         nmp.GetComponent<CanvasGroup>().blocksRaycasts = true;
    53.     }
    54.  
    55.     public void ApplyNumber()
    56.     {
    57.         GameObject nmpcellclone;
    58.         GameObject probinputfieldclone;
    59.         nmpcell = GameObject.FindGameObjectWithTag("NMPCell");
    60.         probinputfield = GameObject.FindGameObjectWithTag("ProbInputField");
    61.         probvalue = GameObject.Find("ProbTransValue").GetComponent<Text>();
    62.         probtitle = GameObject.Find("ProbTransText").GetComponent<Text>();
    63.         numbercelltype = GameObject.FindGameObjectWithTag("numbercelltypefirstorder").GetComponent<Text>();
    64.         for (int pageNumber = 1; pageNumber < (int.Parse(numbercelltype.text) + 1); pageNumber ++)
    65.         {
    66.             probinputfieldclone = Instantiate(probinputfield);
    67.             probinputfieldclone.transform.SetParent(GameObject.Find("1stProbContent").transform, true);
    68.             probinputfieldclone.transform.position = new Vector3(probinputfield.transform.position.x, probinputfield.transform.position.y - (pageNumber * 40), probinputfield.transform.position.z);
    69.             probinputfieldclone.name = ("ProbInputField" + "1." + pageNumber);
    70.             nmpcellclone = Instantiate(nmpcell);
    71.         nmpcellclone.transform.SetParent(GameObject.Find("Canvas").transform, false);
    72.         nmpcellclone.name = ("NMP" + "1." + pageNumber);
    73.  
    74.         }
    75.     }
    76.  
    77.     public void Back1()
    78.     {
    79.         panels = GameObject.Find("Canvas").GetComponentsInChildren<CanvasGroup>();
    80.         foreach (CanvasGroup panel in panels)
    81.         {
    82.             panel.alpha = 0;
    83.             panel.interactable = false;
    84.             panel.blocksRaycasts = false;
    85.         }
    86.         mmp = GameObject.FindGameObjectWithTag("MainMenuPanel");
    87.         mmp.GetComponent<CanvasGroup>().alpha = 1;
    88.         mmp.GetComponent<CanvasGroup>().interactable = true;
    89.         mmp.GetComponent<CanvasGroup>().blocksRaycasts = true;
    90.     }
    91.     public void Next1()
    92.     {
    93.         orderselect = GameObject.FindGameObjectWithTag("OrderDropDown").GetComponent<Dropdown>();
    94.         nmpnow = GameObject.Find("NMP" + (pageCounter + 0.1));
    95.         panels = GameObject.Find("Canvas").GetComponentsInChildren<CanvasGroup>();
    96.         foreach (CanvasGroup panel in panels)
    97.         {
    98.             panel.alpha = 0;
    99.             panel.interactable = false;
    100.             panel.blocksRaycasts = false;
    101.         }
    102.    
    103.  
    104.         {
    105.             nmpnow.GetComponent<CanvasGroup>().alpha = 1;
    106.             nmpnow.GetComponent<CanvasGroup>().interactable = true;
    107.             nmpnow.GetComponent<CanvasGroup>().blocksRaycasts = true;
    108.             pageCounter += 0.1f;
    109.         }
    110.     }
    111. }
    112.  
    113.  

    (I'm in the middle of adding the code for instantiated page's text so there's some unnecessary stuff right now)

    And with this the error (NullReferenceException) is for line 105.

    It would appear that Unity can't find my variable nmpnow. Just to be clear, I click the "Next" button once, which runs the script and updates pageCounter. On that next page, I click "Next" again but for some reason it isn't grabbing the next iteration of the page (and there is indeed another iteration of it--by pausing I can see that there are several clones, as many as I've designated in the previous step).
     
    Last edited: Oct 26, 2015
  6. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
    Code (CSharp):
    1.  
    2.         foreach (CanvasGroup panel in panels)
    3.         {
    4.             panel.alpha = 0;
    5.             panel.interactable = false;
    6.             panel.blocksRaycasts = false;
    7.         }
    8.    
    9.         {
    10.             nmpnow.GetComponent<CanvasGroup>().alpha = 1;
    11.             nmpnow.GetComponent<CanvasGroup>().interactable = true;
    12.             nmpnow.GetComponent<CanvasGroup>().blocksRaycasts = true;
    13.             pageCounter += 0.1f;
    14.         }
    How can this compile? Impossibru! D:
     
  7. jerryjr261

    jerryjr261

    Joined:
    Jun 16, 2015
    Posts:
    31
    This might have something to do with trying to reference value types like object types.

    you could try using properties and accessors.

    Code (CSharp):
    1.     public float pageCounter
    2.     {
    3.         get { return pageCounter; }
    4.         set { pageCounter = value; }
    5.     }
    6.  
     
  8. EternalAmbiguity

    EternalAmbiguity

    Joined:
    Dec 27, 2014
    Posts:
    3,144
    Would I need to change any code other than adding this in at the beginning?
     
  9. jerryjr261

    jerryjr261

    Joined:
    Jun 16, 2015
    Posts:
    31
    yeah, you will need to reference a new float like this:

    Code (CSharp):
    1.  
    2. private float _pageCounter = 0.1f;
    3.     public float pageCounter
    4.     {
    5.         get { return _pageCounter; }
    6.         set { _pageCounter = value; }
    7.     }
    8.  
    then down in where you increment pageCounter you would increment _pageCounter instead:


    Code (CSharp):
    1.    
    2. void Update()
    3.     {
    4.         SetPageCounter();
    5.     }
    6.  
    7.     void SetPageCounter()
    8.     {
    9.         foreach (GameObject go in gameObject)
    10.         {
    11.             _pageCounter += 0.1f;
    12.         }
    13.         print(pageCounter);
    14.     }
    15.  
     
  10. jerryjr261

    jerryjr261

    Joined:
    Jun 16, 2015
    Posts:
    31
    I also notice in line 94 that pageCount is perhaps trying to add a double to a float.
     
  11. EternalAmbiguity

    EternalAmbiguity

    Joined:
    Dec 27, 2014
    Posts:
    3,144
    Apologies for the late reply, but...
    I added the first part of the script, the definitions. Then I added this:

    Code (csharp):
    1. void Update ()
    2.     {
    3.         SetPageCounter();
    4.     }
    5.  
    6.     void SetPageCounter()
    7.     {
    8.         foreach (GameObject page in GameObject.FindObjectsOfType<GameObject>())
    9.         {
    10.             if (page.name.Contains("NMP1."))
    11.             {
    12.                 _pageCounter += 0.1f;
    13.             }
    14.         }
    15.         print(pageCounter);
    16.     }
    I had to change a couple things to get it to specify what I wanted (just certain panels). I don't know if this caused an issue...

    Anyway, I didn't change anything else. And when I ran the scene and input(ted?) the value for "numbercelltype" or the number of panels I would have, the dialog began incrementing upwards constantly. From 0.1, to 0.5, to 0.900001, to 1.3, to 1.7, and so on. I stopped it when it got to 500 or so. So it's clearly not doing what it's supposed to.

    In addition, when I pressed my "Next" button (I'll call it Next1), all I got was a blank page, and a NullReferenceException, at the same line as before. Now, I'll point out that this was for the FIRST time of clicking Next1. Previously, it worked for the first time but didn't work for the second time.

    I also tried changing my variables in my Next1 part of the script to _pageCounter instead of pageCounter, but that didn't do anything.

    So I'm either doing something wrong, or this isn't a solution to my problem. Thanks for the help, though. Any other suggestions?
     
  12. EternalAmbiguity

    EternalAmbiguity

    Joined:
    Dec 27, 2014
    Posts:
    3,144
    Well dang. This was it.

    The weird thing was it wasn't giving me any errors, when it DID give me an error for line 108 when I didn't have an "f." Suppose that should have clued me in to change it on my own, but oh well. Many thanks for the help.

    I'm having another related issue. In my code, lines 66-69 instantiate a number of input fields based on the "numbercelltype" value. However, I've found that they increment, rather than creating ALL of them on every panel. So the first panel only has once instance, the second has two, and so on. I want it to instantiate the maximum amount on every panel. I suspect that I'll be able to figure it out swiftly, but I wanted to mention it here just in case someone already knows the solution.

    Edit: Well that was odd. By simply moving it to its own "for" loop, it solved the problem. Now every panel has the same amount of the input fields.
    Code (csharp):
    1. public void ApplyNumber()
    2.     {
    3.         GameObject nmpcellclone;
    4.         GameObject probinputfieldclone;
    5.         nmpcell = GameObject.FindGameObjectWithTag("NMPCell");
    6.         probinputfield = GameObject.FindGameObjectWithTag("ProbInputField");
    7.         probvalue = GameObject.Find("ProbTransValue").GetComponent<Text>();
    8.         probtitle = GameObject.Find("ProbTransText").GetComponent<Text>();
    9.         numbercelltype = GameObject.FindGameObjectWithTag("numbercelltypefirstorder").GetComponent<Text>();
    10.         for (int pageNumber = 1; pageNumber < (int.Parse(numbercelltype.text) + 1); pageNumber++)
    11.         {
    12.             probinputfieldclone = Instantiate(probinputfield);
    13.             probinputfieldclone.transform.SetParent(GameObject.Find("1stProbContent").transform, true);
    14.             probinputfieldclone.transform.position = new Vector3(probinputfield.transform.position.x, probinputfield.transform.position.y - (pageNumber * 40), probinputfield.transform.position.z);
    15.             probinputfieldclone.name = ("ProbInputField" + "1." + pageNumber);
    16.         }
    17.             for (int pageNumber = 1; pageNumber < (int.Parse(numbercelltype.text) + 1); pageNumber ++)
    18.         {
    19.             nmpcellclone = Instantiate(nmpcell);
    20.         nmpcellclone.transform.SetParent(GameObject.Find("Canvas").transform, false);
    21.         nmpcellclone.name = ("NMP" + "1." + pageNumber);
    22.  
    23.         }
    24.     }
     
    Last edited: Oct 28, 2015
  13. jerryjr261

    jerryjr261

    Joined:
    Jun 16, 2015
    Posts:
    31
    Awesome! I believe I was overthinking it quite a bit sorry to make things more complicated than they needed to be
     
  14. EternalAmbiguity

    EternalAmbiguity

    Joined:
    Dec 27, 2014
    Posts:
    3,144
    Well I wouldn't have figured it out at all without your help, so thanks for that.
     
  15. jerryjr261

    jerryjr261

    Joined:
    Jun 16, 2015
    Posts:
    31
    Sure thing!