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

Problem : object is "null" only in one function...

Discussion in 'Scripting' started by Hishicorne, Aug 10, 2017.

  1. Hishicorne

    Hishicorne

    Joined:
    Jun 25, 2017
    Posts:
    29
    Hey,

    I have actually a problem with my code that I just don't understand : I defined a variable "cookingcontroller" that contain a script. I use this object in a function before - "ReadingAndWriteInSlider", and it work perfectly. But in the second function, it just don't work... Here is my code :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class RecipesBook : MonoBehaviour {
    7.  
    8.     // Bools
    9.     public bool ShowAll = true;
    10.     public bool ShowSweet;
    11.     public bool ShowBitter;
    12.     public bool ShowSour;
    13.     public bool ShowSpicy;
    14.     public bool ShowStarry;
    15.  
    16.     // References
    17.     private CookingSystem cookingcontroller;
    18.     private SaveWriteJSON saveJSONcontroller;
    19.  
    20.     public Dropdown changer;
    21.     public GameObject ButtonMeal;
    22.  
    23.     public Image MealPortrait;
    24.     public Text MealName;
    25.     public Text MealTaste;
    26.     public Text SugarRate;
    27.     public Text SaltRate;
    28.     public Text FatRate;
    29.     public Text MeatRate;
    30.     public Text DamageInfo;
    31.  
    32.     void Start () {
    33.         cookingcontroller = gameObject.GetComponentInParent<CookingSystem>();
    34.         saveJSONcontroller = gameObject.GetComponentInParent<SaveWriteJSON>();
    35.     }
    36.  
    37.     public void ChangePreferenceElements () {
    38.         if (changer.value == 0) {
    39.             ShowAll = true;
    40.             ShowSweet = false;
    41.             ShowBitter = false;
    42.             ShowSour = false;
    43.             ShowSpicy = false;
    44.             ShowStarry = false;
    45.         } else if (changer.value == 1) {
    46.             ShowAll = false;
    47.             ShowSweet = true;
    48.             ShowBitter = false;
    49.             ShowSour = false;
    50.             ShowSpicy = false;
    51.             ShowStarry = false;
    52.         } else if (changer.value == 2) {
    53.             ShowAll = false;
    54.             ShowSweet = false;
    55.             ShowBitter = true;
    56.             ShowSour = false;
    57.             ShowSpicy = false;
    58.             ShowStarry = false;
    59.         } else if (changer.value == 3) {
    60.             ShowAll = false;
    61.             ShowSweet = false;
    62.             ShowBitter = false;
    63.             ShowSour = true;
    64.             ShowSpicy = false;
    65.             ShowStarry = false;
    66.         } else if (changer.value == 4) {
    67.             ShowAll = false;
    68.             ShowSweet = false;
    69.             ShowBitter = false;
    70.             ShowSour = false;
    71.             ShowSpicy = true;
    72.             ShowStarry = false;
    73.         } else if (changer.value == 5) {
    74.             ShowAll = false;
    75.             ShowSweet = false;
    76.             ShowBitter = false;
    77.             ShowSour = false;
    78.             ShowSpicy = false;
    79.             ShowStarry = true;
    80.         }
    81.  
    82.         ReadingAndWriteInSlider ();
    83.     }
    84.  
    85.     void ReadingAndWriteInSlider () {
    86.         foreach(GameObject go in GameObject.FindGameObjectsWithTag("TagMeal")){
    87.             Destroy (go);
    88.         }
    89.  
    90.         int InstantiatedButton = 0;
    91.  
    92.         for (int i = 0; i < cookingcontroller.RecipesList.recipes.Count; i++) {
    93.             bool CanBeBaked = true;
    94.  
    95.             if (cookingcontroller.RecipesList.recipes[i].IceCream > 0 && saveJSONcontroller.SaveData.slot[0].UnlockedIceCream == false) { CanBeBaked = false; }
    96.             if (cookingcontroller.RecipesList.recipes[i].Caramel > 0 && saveJSONcontroller.SaveData.slot[0].UnlockedCaramel == false) { CanBeBaked = false; }
    97.             if (cookingcontroller.RecipesList.recipes[i].Pomelo > 0 && saveJSONcontroller.SaveData.slot[0].UnlockedPomelo == false) { CanBeBaked = false; }
    98.             if (cookingcontroller.RecipesList.recipes[i].Carrot > 0 && saveJSONcontroller.SaveData.slot[0].UnlockedCarrot == false) { CanBeBaked = false; }
    99.             if (cookingcontroller.RecipesList.recipes[i].Cherry > 0 && saveJSONcontroller.SaveData.slot[0].UnlockedCherry == false) { CanBeBaked = false; }
    100.             if (cookingcontroller.RecipesList.recipes[i].Mint > 0 && saveJSONcontroller.SaveData.slot[0].UnlockedMint == false) { CanBeBaked = false; }
    101.             if (cookingcontroller.RecipesList.recipes[i].Honey > 0 && saveJSONcontroller.SaveData.slot[0].UnlockedHoney == false) { CanBeBaked = false; }
    102.             if (cookingcontroller.RecipesList.recipes[i].PeanutButter > 0 && saveJSONcontroller.SaveData.slot[0].UnlockedPeanutButter == false) { CanBeBaked = false; }
    103.             if (cookingcontroller.RecipesList.recipes[i].Coffee > 0 && saveJSONcontroller.SaveData.slot[0].UnlockedCoffee == false) { CanBeBaked = false; }
    104.             if (cookingcontroller.RecipesList.recipes[i].Watermelon > 0 && saveJSONcontroller.SaveData.slot[0].UnlockedWatermelon == false) { CanBeBaked = false; }
    105.             if (cookingcontroller.RecipesList.recipes[i].Wasabi > 0 && saveJSONcontroller.SaveData.slot[0].UnlockedWasabi == false) { CanBeBaked = false; }
    106.             if (cookingcontroller.RecipesList.recipes[i].Yuzu > 0 && saveJSONcontroller.SaveData.slot[0].UnlockedYuzu == false) { CanBeBaked = false; }
    107.  
    108.             if (!ShowAll) {
    109.                 if (ShowSweet && cookingcontroller.RecipesList.recipes[i].Sweet == false) { CanBeBaked = false; }
    110.                 if (ShowBitter && cookingcontroller.RecipesList.recipes[i].Bitter == false) { CanBeBaked = false; }
    111.                 if (ShowSour && cookingcontroller.RecipesList.recipes[i].Sour == false) { CanBeBaked = false; }
    112.                 if (ShowSpicy && cookingcontroller.RecipesList.recipes[i].Spicy == false) { CanBeBaked = false; }
    113.                 if (ShowStarry && cookingcontroller.RecipesList.recipes[i].Starry == false) { CanBeBaked = false; }
    114.             }
    115.  
    116.             if (CanBeBaked == true) {
    117.                 GameObject InstantiatedButtonMeal = Instantiate(ButtonMeal) as GameObject;
    118.                 InstantiatedButtonMeal.transform.parent = GameObject.FindGameObjectWithTag("RecipeListContainer").transform;
    119.                 if (InstantiatedButton == 0) {
    120.                     InstantiatedButtonMeal.GetComponent<RectTransform>().anchoredPosition = new Vector2 (0, -15);
    121.                 } else {
    122.                     InstantiatedButtonMeal.GetComponent<RectTransform>().anchoredPosition = new Vector2 (0, -(25*InstantiatedButton)-15);
    123.                 }
    124.                 InstantiatedButtonMeal.transform.GetChild(0).GetComponent <Text>().text = cookingcontroller.RecipesList.recipes[i].Name;
    125.                 InstantiatedButtonMeal.GetComponent <RecipesBookButton>().IDMeal = i;
    126.  
    127.                 InstantiatedButton ++;
    128.             }
    129.         }
    130.     }
    131.  
    132.     public void SeeThisRecipes (int RecipeID) {
    133.         MealPortrait.GetComponent<Image>().sprite = Resources.Load<Sprite>(cookingcontroller.RecipesList.recipes[RecipeID].AtlasImage);
    134.         MealName.text = cookingcontroller.RecipesList.recipes[RecipeID].Name;
    135.         //MealTaste.text = cookingcontroller.RecipesList.recipes[RecipeID].;
    136.         SugarRate.text = cookingcontroller.RecipesList.recipes[RecipeID].RateSugar + "%";
    137.         SaltRate.text = cookingcontroller.RecipesList.recipes[RecipeID].RateSalt + "%";
    138.         FatRate.text = cookingcontroller.RecipesList.recipes[RecipeID].RateFat + "%";
    139.         MeatRate.text = cookingcontroller.RecipesList.recipes[RecipeID].RateMeat + "%";
    140.         DamageInfo.text = cookingcontroller.RecipesList.recipes[RecipeID].DamagePoint + " (on monsters only)";
    141.     }
    142.  
    143. }
    And here is the code that call the function SeeThisRecipes :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class RecipesBookButton : MonoBehaviour {
    7.  
    8.     // Ints
    9.     public int IDMeal;
    10.  
    11.     // References
    12.     public GameObject accesstobookcontroller;
    13.  
    14.     void Start() {
    15.         Button btn = gameObject.GetComponent<Button>();
    16.         btn.onClick.AddListener(TaskOnClick);
    17.     }
    18.  
    19.     void TaskOnClick() {
    20.         accesstobookcontroller.GetComponent <RecipesBook>().SeeThisRecipes(IDMeal);
    21.     }
    22.  
    23. }
    I have an error at this line : "MealPortrait.GetComponent<Image>().sprite = Resources.Load<Sprite>(cookingcontroller.RecipesList.recipes[RecipeID].AtlasImage);", and it's because "cookingcontroller" is null, but i just don't understand why...
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Copy and post the error you are getting as well. Thanks.
     
  3. Hishicorne

    Hishicorne

    Joined:
    Jun 25, 2017
    Posts:
    29
    Hi ! Here is the error code :

     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    hmm...ok. So you click a button and it calls that method. Got it. Are you certain it's cookingcontroller that is null and not something else you are accessing? You're accessing A few things in cookingcontroller which could also be null.

    cookingcontroller isn't just suddenly going to be null. So if it has a value after you assign it in start, then it will have that value if you aren't changing it.
     
    Last edited: Aug 10, 2017
  5. Hishicorne

    Hishicorne

    Joined:
    Jun 25, 2017
    Posts:
    29
    Well I've done a Debug.Log(cookingcontroller); wich tells me cookingcontroller is null, so I asume this is the error...
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Do you perhaps have two copies of your script in the scene?
     
  7. Hishicorne

    Hishicorne

    Joined:
    Jun 25, 2017
    Posts:
    29
    Of the cooking recipes ? No, I only have one
     
  8. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Is the thing cookingController is attached to also tagged as "TagMeal"?
     
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    For CookingSystems or RecipesBook.
     
  10. Hishicorne

    Hishicorne

    Joined:
    Jun 25, 2017
    Posts:
    29
    They are only one reference of this in the scene

    Nop the cookingcontroller is not attached to the objects tagged "tag meal" - these are buttons
     
  11. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    here is what I would do for testing
    set the CookingSystem to [SerializeField] to view it in unity editor
    and put in a small if statement to

    Code (CSharp):
    1.     // References
    2.     [SerializeField]
    3.     private CookingSystem cookingcontroller;
    4.     [SerializeField]
    5.     private SaveWriteJSON saveJSONcontroller;
    6.  
    7. void Start () {
    8.         cookingcontroller = gameObject.GetComponentInParent<CookingSystem>();
    9.         saveJSONcontroller = gameObject.GetComponentInParent<SaveWriteJSON>();
    10.         if(cookingcontroller == null)
    11.         {
    12.             Debug.Log("failed to set cookingcontroller, via Start");
    13.         }
    14.     }
     
  12. Hishicorne

    Hishicorne

    Joined:
    Jun 25, 2017
    Posts:
    29
    I've just done it and I don't have "failed to set cookingcontroller, via Start" on my console so cookingcontroller is not null. I also see that cookingcontroller exist when I click on the button and that it's supposed to do the SeeThisRecipes () function
     
  13. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    He does mention it works in the other method, which is why it felt like it was a duplicate script error as if one copy was off, but hooked in, then the start wouldn't execute, which would make cookingcontroller null in one copy of the script but not the other.

    So, if it's not a duplicate script error, then you need to see at what point cookingcontroller becomes null.

    You can also add a Debug.Log("cookingController is null here", gameObject); right before the line that creates your null error. Then you can click on that debug message in the console to take you to the gameobject.
     
  14. Hishicorne

    Hishicorne

    Joined:
    Jun 25, 2017
    Posts:
    29
    I've more or less already done it, accessing to cookingcontroller is just impossible, like I've replace all the SeeThisRecipes () function with only a Debug.Log (cookingcontroller); and a Debug.Log (cookingcontroller.WantSour) (a bool in the script CookingSystem)... And unity just don't access to cookingcontroller, just in this function (it works in all other function and never become null in the unity inspector / editor)...
     
  15. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    you need to brake down each of the peace's and find out with is null,

    MealPortrait.GetComponent<Image>().sprite //does it find the Image component? this might need to SpriteRender

    Resources.Load<Sprite>(cookingcontroller.RecipesList.recipes[RecipeID].AtlasImage); //is AtlasImage a typo?
    Resources.Load<Sprite>() needs a string. it's the directory path and filename of the image you are using. IE : Resources.Load<Sprite>("Sprites/misc/backgrounds/wallBackground1.png");

    do a Debug.Log(cookingcontroller.RecipesList.recipes[RecipeID].AtlasImage); to see what path it's using
     
  16. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    my bad it's not the file name, just the directory

    Resources.Load will search for a directory in Assets/Resources.

    If you want to put it to Sprites directory then put it inside Resources (ex. Assets/Resources/Sprites).

    Then you can just load it like this:

    Sprite myFruit = Resources.Load <Sprite> ("Sprites/Graphics_3");
     
  17. Hishicorne

    Hishicorne

    Joined:
    Jun 25, 2017
    Posts:
    29
    It find everything except the cookingcontroller. Debug.Log(cookingcontroller.RecipesList.recipes[RecipeID].AtlasImage); lead to the same error. I know this line of code work pretty well - I've already tested it outside the code with a static RecipeID (wich is 0)

    Also I just tried something, displaying a variable bool that is on the script : it just display on the console the INITIAL value of the bool. If I change this value and then call the function, it still display "false" despite the fact it's true. I just don't understand what's going on with the function, I may have an idea, i'll try to check if if I call the at start and on the update function, it work or not.. ?
     
  18. Hishicorne

    Hishicorne

    Joined:
    Jun 25, 2017
    Posts:
    29
    Ok so i've tried. Calling the function without clicking on the button (like I've added a SeeThisRecipes (0); to the ReadingAndWriteInSlider function - It work. So I now assume that it's the RecipesBookButton script that provoke the error....
     
  19. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    So using this debug
    Debug.Log("cookingController is null here", gameObject); //Note the added gameObject part

    When it prints it targets the proper gameobject and cookingcontroller isn't null but when you debug cookingcontroller right after, cookingcontroller is null?

    If you are getting default values in this method but you change it elsewhere, it still points to a duplicate script error. Basically working with two instances of the script.

    Code (CSharp):
    1. accesstobookcontroller.GetComponent <RecipesBook>().SeeThisRecipes(IDMeal);
    Means this RecipesBook is probably not the same one that you are calling elsewhere.
     
  20. Hishicorne

    Hishicorne

    Joined:
    Jun 25, 2017
    Posts:
    29
    Ok. You'r a genius & you'r true. I've just changed the RecipesBookButton code to be :
    GameObject.FindWithTag("GameController").GetComponent <RecipesBook>().SeeThisRecipes(IDMeal);

    And it work. The problem was that RecipesBookButton looked for a GameController object that wasn't instantiated and not the one that is one the scene.

    Thank everyone for your help !