Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by icetray21 · Mar 03, 2018 at 03:34 PM · listresourcespathselements

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.

  1. 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);
             }
      
         }
     
          }
    
    
    
    
    
    
    
    
    
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chik3r · Mar 03, 2018 at 03:44 PM 0
Share

Are you sure you have more things on the list

avatar image icetray21 Chik3r · Mar 04, 2018 at 05:51 AM 0
Share

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

avatar image icetray21 Chik3r · Mar 04, 2018 at 06:07 AM 0
Share

these pics show the list before start and then after I call the method

capture-00.jpg (33.4 kB)
capture-01.jpg (47.2 kB)
avatar image hexagonius · Mar 04, 2018 at 06:40 AM 0
Share

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

avatar image icetray21 hexagonius · Mar 04, 2018 at 08:46 AM 0
Share

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???

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

79 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Trying to avoid repeating elements on list (random.range) 1 Answer

C# Displaying List Elements in Multiples 1 Answer

How to get a list of all the GUI elements in a scene? 1 Answer

Variables inside an element list 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges