• 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 ahsen35813 · Aug 15, 2020 at 08:59 PM · arrayarrays

Arrays not incrementing as needed. Any help will be greatly appreciated.

I am trying to create a very basic machine learning program which works by randomly changing values, and keeping the random iteration which did best, and evolving further from that. The way my code works, is that there is a base array called "current[]" which is supposed to update to reflect the best current array of numbers for controlling the player. I also have "alpha[]" and "beta[]" which are the arrays which have a randomly selected value increased or decreased to test different iterations. This is where I run into issues.

I have tried various different ways of doing this, but nothing seems to work. Below are some things I have tried. (P.S.`increment` is an int which is declared at the start of the code. randomNum does not have any value assigned, and increment = 1. In the first code, randomNum is a float, and in the second code, it is an int)

     randomNum = Random.Range(1, 6);
 
     alpha = current;
     beta = current;
 
     if (randomNum > 0 && randomNum < 1)
     {
         alpha[1] += increment;
         beta[1] -= increment;
     }
     else if(randomNum > 1 && randomNum < 2)
     {
         alpha[2] += increment;
         beta[2] -= increment;
     }
     else if (randomNum > 2 && randomNum < 3)
     {
         alpha[3] += increment;
         beta[3] -= increment;
     }
     else if (randomNum > 3 && randomNum < 4)
     {
         alpha[4] += increment;
         beta[4] -= increment;
     }
     else if (randomNum > 4 && randomNum < 5)
     {
         alpha[5] += increment;
         beta[5] -= increment;
     }
     else if (randomNum > 5 && randomNum < 6)
     {
             alpha[6] += increment;
             beta[6] -= increment;
     }

I had tried this first and did the above code as a desperate attempt:

         randomNum = (int)Mathf.Round(Random.Range(1, 6));
 
         alpha = current;
         beta = current;
 
         alpha[randomNum] += increment;
         beta[randomNum] -= increment;

And the part that really confuses me, is that if I do alpha[1] += increment;, it works just fine. I have been trying to fix this with various things for over a week now, and any feedback woud be very much appreciated.

There are no errors, and Visual Studio has no problem compiling the code, but the output numbers just don't make sense. The values of all three arrays are always "0, 0, 0, 0, 0, 0, 0" and I can't figure out why. If you need any more my code to understand the problem, please let me know because this is not the whole code and it is possible something else may be causing the issue, although I traced it back to this section.

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

2 Replies

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

Answer by Eno-Khaon · Aug 16, 2020 at 12:34 AM

Your original version looks right, except for one major detail:

Arrays are passed by reference rather than by value.
It's not that the contained values in the array aren't changing. You just keep setting them back to zero.

Here's a breakdown of what is currently happening:

 current = new int[7];
 alpha = current;
 beta = current;
 
 // example
 alpha[1] += 5; // current[1](value: 0) + 5 = 5
 beta[1] -= 5;  // current[1](value: 5) - 5 = 0


If you're looking to make copies of the current array's contents, you'll need to handle it a bit differently first:

 int arrayLength = current.Length; // Creating a temporary variable for reuse
 alpha = new int[arrayLength];
 System.Array.Copy(current, alpha, arrayLength);
 beta = new int[arrayLength];
 System.Array.Copy(current, beta, arrayLength);


At a glance, however, it's hard to tell what the purpose of your alpha and beta arrays is meant to be, so the solution you're looking for could also easily fall outside the option of copying the array's contents first.

Comment
Add comment · Show 4 · 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 ahsen35813 · Aug 16, 2020 at 05:51 PM 0
Share

Yes, I am trying to make a copy of the current[] array's contents. The code shown here runs every 10 seconds so the both alpha[] and beta[] need to be modified to be identical to current[] at the start of the shown code. I had not realized that you had to use System.Array.Copy() to copy an array. I will try that, and let you know what happens. Thank you!

avatar image ahsen35813 · Aug 16, 2020 at 07:59 PM 0
Share

If I understand you correctly, are you saying that if I defined alpha = current and beta = current once, then modifying the values of alpha will change the values of beta and vice versa? If not, please explain as I am quite confused.

avatar image Eno-Khaon ahsen35813 · Aug 17, 2020 at 01:58 AM 0
Share

(Sorry for not responding to this sooner)

Yep, By setting alpha[] and beta[] equal to current[] in that manner, all three of them will reference the same array of values in memory. Changing any component value of the three changes the same value held by all three of them.

That's why you need completely separate arrays to hold the data, then copy the data from one to another.

 // single
 alpha[element] = current[element];
 beta[element] = current[element];
 
 // all, make certain arrays are the same size beforehand
 // or adapt as necessary
 // Heavy-handed example:
 System.Array.Copy(current, alpha, $$anonymous$$athf.$$anonymous$$in(current.Length, alpha.Length));
 System.Array.Copy(current, beta, $$anonymous$$athf.$$anonymous$$in(current.Length, beta.Length));


Furthermore, it's worth noting that it wouldn't be a one-way change if you set the array's reference to current.

 current = new int[7]; // current = Array1
 alpha = current; // alpha = current = Array1
 alpha[0] = 4; // current[0] = 4
 beta = new int[7]; // beta = Array2
 alpha = beta; // alpha = beta = Array2
 alpha[0] = 5; // beta[0] = 5
 alpha = new int[7]; // alpha = Array3
 
 // final states:
 // current = {4, 0, 0, 0, 0, 0, 0}
 // alpha = {0, 0, 0, 0, 0, 0, 0}
 // beta = {5, 0, 0, 0, 0, 0, 0}


Despite only changing a value inside alpha at any given time, this example used the references to other arrays to change their contents before assigning alpha its own new array in the end.

avatar image ahsen35813 · Aug 16, 2020 at 08:05 PM 0
Share

Thank you very much for your help. I replaced the alpha = current; and beta line with your code, and reverted back to my original code, and it now functions perfectly. Thank you very much for you time. I really appreciate the help.

avatar image
0

Answer by RaptorRush · Aug 15, 2020 at 09:57 PM

VS can compile the code because it isn’t a syntax problem, is a logical error.

 if (randomNum > 0 && randomNum < 1)

In this line (and in the others) you make a conditional that is always going to be false. An integer will never be greater than zero and less than one at the same time.


Also, arrays are Zero-based;

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 ahsen35813 · Aug 16, 2020 at 05:43 PM 0
Share

Yes, I know that arrays are zero based. The reason it's not in the code, is because alpha[0] for example is being used to store the AI Agent's score. I apologize for not specifying it in the original post, but randomNum is a float in the first set of code. I have made that correction in the question. If I had modified an int with Random.Range();, I would get a syntax error so that was not the issue.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

How to set parents of all objects in an array? 1 Answer

Activating Bool Array in sequence. 2 Answers

accessing gameObject script from an array 3 Answers

Passing an array to a function by reference? 1 Answer

Help with for loop and arrays 3 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