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),
};
}
}