Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 pface444 · Jul 28, 2015 at 10:05 PM · nullreferenceexceptionstringcopycontainscontent

List is empty when used in other method

     public List<string>copy = new List<string> ();


Below I have a method that works, it receives the List AllowedActionst and I can use it in this method. However how do I use this List in another method in the same class?

   public void BettingRound (NetworkPlayer actors, int Xpos, int Ypos,  List AllowedActionst, int bet) {
             //var foundScript = GetComponent<AllowedActions> () as AllowedActions;
     
     
             List<string> copy = new List<string>(AllowedActionst);
     
             foreach (string i in copy) { Debug.Log(i + "copyyyyy ");}


For example I tried using the AllowedActionst List in the below method in the same class, but the list is always giving a null reference it is empty.

I tried therefore to copy the AllowedActionst List into a new List named copy and made it public. But still the same issue, if I use if (copy.contains....) the list is always empty. Even though the Debug.Log above does say it has 4 strings in copy however as you can see below that Debug.Log always shows null. In other words how do I use the contents of the AllowedActionst List in the below sendBetToServer() method so that it still has the same contents? I thought making it public by declaring it outside of the methods would make it work even though it is initalising in the BettingRound method.

Hope this makes sense.

 public void sendBetToServer() {
         BetButtonPressed = true;
         string ActionActor = "Bet";
 
 
         foreach (string i in copy) { Debug.Log(i + "this always shows an empty List/ null reference???? ");}
         
         //List<Player> Activeplayerss = foundScript2.getActivePlayers(); 
 
 
 
     //    var foundScript55 = gameObject.GetComponent<AllowedActions>();
 //        List<string> AllowedActio = foundScript55.GetAllowedActions ();
 
         GameObject fiets8 = GameObject.Find ("clone2");
         myslider = fiets8.GetComponent<Slider> ();
 
 
         if (copy.Contains ("Raise")) {    GameObject fiets1 = GameObject.Find ("clone1");
             fiets1.SetActive (false);}
         if (copy.Contains ("Raise")) {GameObject fiets2 = GameObject.Find ("clone2");
             fiets2.SetActive (false);}
         if (copy.Contains ("Check")) {GameObject fiets3 = GameObject.Find ("clone3");
             fiets3.SetActive (false);}
         if (copy.Contains ("Fold")) {GameObject fiets4 = GameObject.Find ("clone4");
             fiets4.SetActive (false);}
         if (copy.Contains ("Call")) {GameObject fiets5 = GameObject.Find ("clone5");
             fiets5.SetActive (false);}



Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by nisovin · Jul 28, 2015 at 10:27 PM

You're declaring a new copy variable in BettingRound(), which means you aren't using the class variable. To help explain further:

 public class Example {
     
     public int number = 10;
     
     public void test1() {
         int number = 20; // declaring a new variable with the same name, which only exists within this function
     }
     
     public void test2() {
         number = 20; // modifying the class variable
     }
     
     public void test3() {
         Debug.Log(number); // prints 10
         test1();
         Debug.Log(number); // prints 10 still, because the class variable didn't change
         test2();
         Debug.Log(number); // prints 20 now, because we changed the class variable
     }
     
 }
Comment
Add comment · Show 2 · 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 pface444 · Jul 29, 2015 at 09:07 AM 0
Share

Thanks for the answer, I will have a play around with it, when Bettinground is called it will receive the List as an argument, is there a way to make that a class variable?

So let's say I was not declaring it in the bettinground but simply use it directly like so: AllowedActionst.Contains.....

And put it as a class variable: public List Allowedactionst = new List;

Could I then use it in the methodsendBetToServer as per above?

I will give it a try, I have a feeling I already tried this.

Let you know if it works this afternoon...

avatar image pface444 · Jul 29, 2015 at 09:53 AM 0
Share
 public List <string> AllowedActionClass = new List<string> ();

So I tried the above creating a public class variable.... I then used thebelow. AllowedActionClass= AllowedActionst; (similar as Number = 20?? rather then int number = 20??)

 public void BettingRound (NetworkPlayer actors, int Xpos, int Ypos,  List<string> AllowedActionst, int bet) {
         //var foundScript = GetComponent<AllowedActions> () as AllowedActions;
 
         AllowedActionClass = AllowedActionst;


However still in SendBetToServer, the Debug.Log is empty.

 public void sendBetToServer() {
         BetButtonPressed = true;
         string ActionActor = "Bet";
 
 
         foreach (string i in AllowedActionClass) { Debug.Log(i + "werkt dit nu wellll???? ");}
         
         //List<Player> Activeplayerss = foundScript2.getActivePlayers(); 
 
 
 
     //    var foundScript55 = gameObject.GetComponent<AllowedActions>();
 //        List<string> AllowedActio = foundScript55.GetAllowedActions ();
 
         GameObject fiets8 = GameObject.Find ("clone2");
         myslider = fiets8.GetComponent<Slider> ();
 
 
         if (AllowedActionClass.Contains ("Raise")) {    GameObject fiets1 = GameObject.Find ("clone1");
             clone1.SetActive (false);}

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

22 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

Related Questions

I am Trying to find what position a Space is in my Array and get them to a array of floats 2 Answers

string.Contains() to work with string with Chinese characters? 1 Answer

Converting Int to String results in NullReferenceException? 2 Answers

NullReferenceException when transform.Find(string) STILL 1 Answer

Instantiating class creating copies with same instance ID of 0 0 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