0

I'm trying to use a subroutine from one script into another in C# Unity however I've worked out that it skips a certain if statement regardless of what I try.

        public int GetID(string title)
        {
            BackPack holderB = backpack.Find(backpack => backpack.title == title);
            if (holderB != null)
            {
                Debug.Log("ID:" + holderB.id);
                return holderB.id;
            }
            Shoes holderS = shoes.Find(shoes => shoes.title == title);
            Debug.Log("ID" + holderS.id);
            return holderS.id;
        }

When I pick up a shoes item the program runs fine and outputs the correct ID however if I pick up a BackPack Item it does not and gives a null error when it tries to figure out holderS.id with incorrect data. I've tried changing (holderB != null) to (holderB.id != null) but a integer cannot be null and switching the backpack and shoes around creates the same problem in reverse. The only other way that come to mind is checking each title manually but I of course do not want to do that.

FULL

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

    

    public class Item : MonoBehaviour
    {

        public int id;
        public string title;

        public Item()
        {

        }
    }


    public class Shoes : Item
    {
        public new int id;
        public new string title;
        public int Speed;
        public float Drag;


    public Shoes(int id, string title, int Speed, float Drag)
        {
            this.id = id;
            this.title = title;
            this.Speed = Speed;
            this.Drag = Drag;

    }

        public Shoes(Shoes shoes)
        {
            this.id = shoes.id;
            this.title = shoes.title;
            this.Speed = shoes.Speed;
            this.Drag = shoes.Drag;
        }
    }

    public class BackPack : Item
    {
        public new int id;
        public new string title;
        public float Gravity;

        public BackPack(int id, string title, float Gravity)
        {
            this.id = id;
            this.title = title;
            this.Gravity = Gravity;
        }

        public BackPack(BackPack backpack)
        {
            this.id = backpack.id;
            this.title = backpack.title;
            this.Gravity = backpack.Gravity;
        }
    }
    public class ItemDataBase : MonoBehaviour
    {
        public List<Shoes> shoes = new List<Shoes>();

        public List<BackPack> backpack = new List<BackPack>();
        
        //
        // Add this. This code runs when you create an 
        // instance of this class (var db = new ItemDataBase();)
        //
        public ItemDataBase() 
        { 
           this.Awake(); 
        }

        void Awake()
        {
            BuildShoesDataBase();
            BuildBackPackDataBase();
        }

        public int GetID(string title)
        {
            BackPack holderB = backpack.Find(backpack => backpack.title == title);
            if (holderB != null)
            {
                Debug.Log("ID:" + holderB.id);
                return holderB.id;
            }
            Shoes holderS = shoes.Find(s => s.title == title);
            Debug.Log("ID" + holderS.id);
            return holderS.id;
        }

        public int GetShoesSpeed(string title)
        {
            
            Shoes holderS = shoes.Find(shoes => shoes.title == title);
            return holderS.Speed;
        }

        public float GetShoesDrag(string title)
        { 
            Shoes holderS = shoes.Find(shoes => shoes.title == title);
            return holderS.Drag;
        }

    public float GetBackPackGravity(string title)
        {
            BackPack holderB = backpack.Find(backpack => backpack.title == title);
            return holderB.Gravity;
        }


        void BuildShoesDataBase()
        {
            shoes = new List<Shoes>()
        {
            new Shoes(4, "SlippySlides(Clone)", 70, 0.01f),
            new Shoes(5, "GrippedTrainers(Clone)", 35, 0.175f),
            new Shoes(6, "Wano11s(Clone)", 55, 0.02f),

        };
        }

        void BuildBackPackDataBase()
        {
            backpack = new List<BackPack>()
        {
            new BackPack(1, "Cannon(Clone)", 0.65f),
            new BackPack(2, "Auto(Clone)", 0.75f),
            new BackPack(3, "BigBang(Clone)", 0.5f),

        };
        }
    }
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Zain
  • 9
  • 2
  • 3
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Jonathan Barraone Jan 21 '22 at 00:36
  • @JonathanBarraone Hey, unfortunately no as I know what the error means I just cant figure out how to fix it. – Zain Jan 21 '22 at 00:40
  • 1
    @Zain _"I know what the error means I just cant figure out **how to fix it**"_ - well that's exactly what the duplicate link will tell you –  Jan 21 '22 at 00:41
  • You are not checking if holderS is null. And BTW you are writing it as x.Find(x => x.title == ...). Use a different alias such as y. – Cetin Basoz Jan 21 '22 at 00:41
  • @MickyD I'm asking what can I use instead of (holderB != null) basically. sorry for any confusion. – Zain Jan 21 '22 at 00:43
  • @CetinBasoz Yes but it shouldn't matter as there's only two possible outcomes either one of the holders has to be not null. – Zain Jan 21 '22 at 00:45
  • It is what you are saying but I would check. Anyway, you are writing it wrong as I said, is this your real code? – Cetin Basoz Jan 21 '22 at 00:48
  • @CetinBasoz yes this is my real code but since I'm not too good at this type of coding idrm if it's technically wrong but still works. – Zain Jan 21 '22 at 00:51
  • And also you are saying "if I pick up a BackPack ... when it tries to figure out holderS.id." HolderS doesn't exist, does it? It shouldn't work and give an error, I am stumped why t works. – Cetin Basoz Jan 21 '22 at 00:52
  • @CetinBasoz exactly when I pick up a backpack the holderB is actually assigned but the if statement check doesn't work and it carries on only to get a null error on holderS. – Zain Jan 21 '22 at 00:56
  • Sorry but I can't even understand why you are not getting "CS0136 A local or parameter named 'backpack' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter" error. Can't you at least try changing it to something like: BackPack holderB = backpack.Find(bp => bp.title == title); (and also same for the shoes). – Cetin Basoz Jan 21 '22 at 00:59
  • @CetinBasoz changing it does work still but it doesn't change the fact I cant check if the holder is assigned or not. – Zain Jan 21 '22 at 01:03
  • @CetinBasoz my question was also closed but is still active so ig you're the only one helping for now. – Zain Jan 21 '22 at 01:04
  • @CetinBasoz I've also added my full code too. – Zain Jan 21 '22 at 01:08
  • You are not initializing your lists. Both HolderB and HolderS were null. Try adding a constructor there: public ItemDataBase() { this.Awake(); } – Cetin Basoz Jan 21 '22 at 01:31
  • @CetinBasoz I'm not going to lie.. idk what that means. – Zain Jan 21 '22 at 01:36
  • In your public class ItemDataBase : MonoBehaviour class add the method I gave you above. If you are using and IDE like VS, then also typing ctor [TAB] [TAB] would create one for you. In that case, put "this.Awake();" there. I will try editing your question to add it there, you can check from there. – Cetin Basoz Jan 21 '22 at 10:37
  • I edited your "FULL" code to include that. Check before void Awake() { ... – Cetin Basoz Jan 21 '22 at 10:42

0 Answers0