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 /
  • Help Room /
avatar image
0
Question by ShaveTHEbacoN · Nov 07, 2015 at 02:11 PM · guiarraylabelsimple fix

Find a string in an array from another script

I have two objects, one contains a script that generates an array of player stats. I would like to use the second object to generate gui labels of the player data. The aim is to create a way for the player to browse through multiple player stats.

here is the code for the array generation. using UnityEngine;

 public class PlayerGenerator : MonoBehaviour {
     public class player
     {
         public string name; 
     }
     public TextAsset namelist; //Set namelist as asset
     public player[] playerpool = new player[51]; //create an array

     void Start() {
 
         var names = namelist.text.Split("\n"[0]);
 
         float nameValue;

         for (int i = 1; i < 50; i++)
         {
             nameValue = UnityEngine.Random.Range(0, 49);
             playerpool[i].name = names[(int)portraitValue];
             }
         }
     }
 

and here is the code for the script to show the labels.

 public class GUImanager : MonoBehaviour {
     public PlayerGenerator playerArray;
 
 
     private void OnGUI()
     {
         string name = playerArray.playerpool[0].name;
         GUI.Label(new Rect(460, 70, 150, 25),name);       
     }

Thanks any help greatly appreciated.

Comment
Add comment · Show 3
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 ShaveTHEbacoN · Nov 07, 2015 at 02:16 PM 0
Share

sorry playerpool[i].name = names[(int)portraitValue]; should be playerpool[i].name = names[(int)nameValue];

avatar image ShaveTHEbacoN · Nov 07, 2015 at 02:17 PM 0
Share

The error I am getting is NullReferenceException: Object reference not set to an instance of an object GUImanager.OnGUI () (at Assets/Scenes/GUImanager.cs:10)

avatar image pekalicious · Nov 12, 2015 at 06:09 AM 0
Share

Try changing Start() to Awake(). Are you still getting errors?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by pekalicious · Nov 12, 2015 at 08:49 AM

This line will not create players, it will only create an array that will eventually contain players: public player[] playerpool = new player[51]; //create an array

So when you do this: player[0].name you are saying "Get the player at index 0 from the array, and set its name". But since you haven't actually set any player at that index, it crashes.

To set the player, you simply create one and put it in the array:

 for (int i = 1; i < 50; i++)
 {
     nameValue = UnityEngine.Random.Range(0, 49);
     playerpool[i] = new player(); // Create the player and add it to the array
     playerpool[i].name = names[(int)portraitValue];
 }

Also, I'm not sure if OnGUI is called after Start, so you might want to try changing Start to Awake, just to be sure that you have created the array before anybody tries to access it.

Comment
Add comment · Show 1 · 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
avatar image bubzy · Nov 12, 2015 at 09:34 PM 0
Share

this is what my brain wouldnt let me say this morning before work :D

avatar image
0

Answer by bubzy · Nov 12, 2015 at 07:49 AM

 using UnityEngine;
 using System.Collections;
 
 public class PlayerGenerator : MonoBehaviour {
 
     [System.Serializable] //just to show in editor window 
     public class player
     {
         public string name; 
         
         public player(string _name)
         {
             name = _name;
         }
     }
 
 
     public player[] playerpool = new player[20] ;
 
     void Start() {
         for (int i = 0; i < 10; i++) {        
             playerpool[i] = new player("Player " + i);
         }
 
     }
 }


this works, sorry im out of time to try and work out the problem with yours this morning (work calls) hope this helps

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

45 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

Related Questions

Disabling random GUI Button from array 0 Answers

separate the items of an array with lines 1 Answer

Instantiated Buttons to have separate On-click functions 0 Answers

Assigning a variable to my label 1 Answer

How do you escape the angle brackets in rich text? 4 Answers


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