- Home /
My List only works on last element when I use it to find a path to open into my Resources folder.
I haven't been able to figure out what is going wrong. I am passing in a random number between 1 and the .count of the list so when I continue to add more character races to the game I woun't have to keep adding more code. The list gets its data from a text file I'm storing in Resources on one script then from another script I'm trying to pick the path where I need to load Scriptable objects. The problem is the path only works if its the last element. If someone can give my code a look and let me know where I'm going wrong it would be extremely helpful. I'm pretty new to coding, so simple explanations and easy fixes would be best. In C# would also be most helpful.
This is the First Script where it makes the list.
using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Linq;
[RequireComponent(typeof(TextAsset))] public class TextInfo_Sub_Races : MonoBehaviour { public List<string> textArray; public TextAsset textInfo; [SerializeField] public string FileName = "Sub_Race_Info_text_asset"; public List <string> refToSubRaceTxtArray; // Use this for initialization private void Start() { textInfo = Resources.Load("Races_Classes_and_Base_Units/" + FileName) as TextAsset; //textArray = textInfo.text.Split('\n').ToList(); textArray = textInfo.text.Split('\n').ToList(); refToSubRaceTxtArray = textArray; } } ***And this is the Second Script*** using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Linq; public class CreateNewPlayerCharacter : MonoBehaviour { private TextInfo_For_ScriptableObjects textInfoScript; // reference to textInfo script private TextInfo_Sub_Races textInfoSubRaces; // reference to textInfo script public Character_Races [] raceTest; // a test, delete when finished public List<string> subRaceInfo; private void Awake() { textInfoScript = GetComponent<TextInfo_For_ScriptableObjects>(); textInfoSubRaces = GetComponent<TextInfo_Sub_Races>(); } private void Update() { if (Input.GetMouseButtonDown(1)) { CreateNewCharacter(); } } private void CreateNewCharacter() { subRaceInfo = textInfoSubRaces.textArray; //subRaceInfo = textInfoSubRaces.refToSubRaceTxtArray; int sizeOfRaceListInfo; sizeOfRaceListInfo = subRaceInfo.Count; int randomSubRace = (Random.Range(1, sizeOfRaceListInfo)); // this is just for getting random path in the races folder string whichSubRace = subRaceInfo[randomSubRace]; raceTest = Resources.LoadAll<Character_Races>("Races_Classes_and_Base_Units/Races/" + whichSubRace); // this is the path which works when its the last element in the list foreach (Character_Races races in raceTest) { Debug.Log(races.name); } } }
I am pretty sure there are more. I can check the list in the inspector and it shows the other elements exactly as they should be
these pics show the list before start and then after I call the method
You should start learning how to debug, cause code problems appear all the time. first of all there's Debug.Log. you're not sure if a value is set, different, not what you expected? Print it to the console with it.
The code looks right to me up to the point where you assign whichSubRace a value, but I could be wrong. $$anonymous$$aybe you debug that to see if that value really is getting set and randomly different each time. If that's fine, something is wrong with the resources path and names.
Then there's breakpoints. The quicker way for debugging, because you don't need to alter your code. VS:
https://unity3d.com/de/learn/tutorials/topics/scripting/debugging-unity-games-visual-studio $$anonymous$$onoDevelop:
https://unity3d.com/de/learn/tutorials/topics/scripting/monodevelops-debugger
I haven't tried the debugger tool, but I have done debug.log(whichSubRace); and it does show it changed to the correct path name. It is possible that it's my path data somehow is the problem but its pretty simple though it looks like this so far
Sub Races
Elves_Sub-Races
Human_Sub-Races
it is in a text file and the last path works is the weird thing. As long as the random number picks the last element even if I move them around or change how long or short the list is the last path works. I thought I might have something wrong with my list overriding so only the last elements variables worked but as I said the debug.log shows its changing so not really sure what I'm doing wrong.
Could it be the paths don't work because of how they are split by lines???
Answer by icetray21 · Mar 05, 2018 at 10:17 AM
I found the solution thanks for all your help
string whichSubRace = subRaceInfo[randomSubRace].Trim(); // needed to trim for removing the extra white spaces.