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
1
Question by Foxthegreat · Oct 01, 2019 at 09:55 PM · arrayint

How do I modify values in an array from a different C# script ?

Hi . so , i'm trying to generate an array of int's in Managerscript.cs and then pass it's values to another gameobject with another script called AIgen2.cs and no method seems to work Here's some code :

 public class Managerv2 : MonoBehaviour
 
 {
     public GameObject Agent;
     public int numberofagents;
     public bool startbool;
     public GameObject[] agentlist;
     
     public int[] movementvector;
     public int[] rotationvector;
     public Rigidbody[] rb;
     public int numberofiterations;
     // Start is called before the first frame update
     void Start()
     {
         numberofagents = 5;
         numberofiterations = 300;
         startbool = false;
         agentlist = new GameObject[numberofagents];
        
         movementvector = new int[numberofiterations];
         rotationvector = new int[numberofiterations];
         rb = new Rigidbody[numberofagents];
 
     }
 void GenerateMovementandRotation()
     {
         for (int i = 0; i <numberofiterations; i++)
         {
             int speedfactor = Random.Range(0, 16);
           
             movementvector[i] = speedfactor;
 
             int rotfactor = Random.Range(-360, 360);
             rotationvector[i] = rotfactor;
         }       
     }
 void StartGeneration()
     {
         for (int i = 0; i < numberofagents; i++)
         {
             GenerateMovementandRotation();
             for (int j = 0; j < numberofiterations; j++)
             {
                 AIgen2 agentz = agentlist[i].GetComponent<AIgen2>();
                 agentz.movementvector2[j] = movementvector[j];
 
                 //agentlist[i].GetComponent<AIgen2>().rotationvector2[j] = rotationvector[j];
             }
         }
     }

The agentlist[i] it's already build and has it's values . Everything works ok , no errors except the fact that none of the values are transferred from rotationvector to rotationvector2

 [[[ agentlist[i].GetComponent().rotationvector2[j] = rotationvector[j];  ]]]

I did check that the array movementvector and rotationvector have values in them before trying to pass them to movementvector2 and rotationvector2 .

Second script

     public class AIgen2 : MonoBehaviour
     {
         public int k = 0;
         public bool startgeneration = false;
         public GameObject goal;
         public GameObject manager;
         public Managerv2 managerscript;
         public int numberofiterations;
         public int numberofagents;
         public int[] movementvector2;
         public int[] rotationvector2;
         public Rigidbody rb;
 }
 
     private void Awake()
     {
                 manager = GameObject.FindGameObjectWithTag("manager");
                 managerscript = manager.GetComponent<Managerv2>();
                 numberofagents = managerscript.numberofagents;
                 numberofiterations = managerscript.numberofiterations;
                 movementvector2 = new int[numberofiterations];
                 rotationvector2 = new int[numberofiterations];
                 rb = GetComponent<Rigidbody>();
                 goal = GameObject.FindGameObjectWithTag("goal");
             }
         }

All the arrays have the same length . I just can't . help me out please

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 tormentoarmagedoom · Oct 02, 2019 at 08:04 AM

Hello there. I think you have some confusion about scriping. Lets say you have this array in script A, that is in ObjectA:

 float[] Array1;

If you want to refear to that array, all the array, (not one value of it), dont have to declare the " [ ] " symbols, because that is only to specify one element inside the array.

Lets make a 2nd array in the same Script A that will be exactly the same as Array1

 float [] Array2 = Array1;

Now, array 2 its exactly the same as Array1, same lenght, same values, they all in ObjectA.

So now, lets imagine we have a ObjectB with script B and want to make a Array3 and Array4 copies of the arrays of scriptA:

  float[] Array3 = ObjectA.GetComponent<ScriptA>().Array1;
  float[] Array4 = ObjectA.GetComponent<ScriptA>().Array2;

As you see, i used the "[ ]" symbols only for declare the array. but not for define it, (because the variable called Array3 is an array, is not just a value) We could make it also like this to be more "realistic":

 float[] Array3;
 float[] Array4
 GameObject ObjectA;
 
 void Start()
 {
 ObjectA = GameObject.Find("ObjectA");
 Array3 = ObjectA.GetComponent<ScriptA>().Array1;
 Array4 = ObjectA.GetComponent<ScriptA>().Array2;
 }

When you use the "[ ]" symbols, is to refear a value inside an array. If you dont use the symbols you are refearing to whole array. I expect you to understand all, if not, please ask again, specify what you dont understand

Bye!

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 Foxthegreat · Oct 02, 2019 at 12:06 PM 0
Share

i'll try that too when i get home . Thanks

avatar image Foxthegreat · Oct 02, 2019 at 01:41 PM 0
Share

Your stuff worked but let me explain why it wasn't working for me . what i did wrong is :

 managescript2.cs:
 float [] array1=new float[n]; // THIS HAS VALUES I NEED 
 GetComponent<aigen2>().array2 = array1;
 
 aigen2.cs
 float []array2=new float[n];  /THIS IS WHERE THE VALUES need TO BE TRANSFERRED

Ins$$anonymous$$d i should have done

 managescript2.cs:
     float [] array1=new float[n]; // THIS HAS VALUES I NEED 
     
     aigen2.cs
     float []array2=new float[n];  /THIS IS WHERE THE VALUES need TO BE TRANSFERRED
 array2 =GetComponent<managerscript2>().array1 ;


avatar image Foxthegreat · Oct 02, 2019 at 01:43 PM 0
Share

You are supposed to move the values to the script that has the array you need filled ins$$anonymous$$d of trying to access and modify it(the array) from another script .

avatar image tormentoarmagedoom Foxthegreat · Oct 02, 2019 at 03:24 PM 0
Share

so what? You get what you want? or not?

The thing is to wknow what is beeing executed first.

when you do

 new float[n]

its changing all values of the array to 0. so you must be sure this is done before you set all the values, or do not define the array, just declare it.

declare:

 float [] array1;

define:

 array1 = new float[n];

You can just declare it and dont define, so when the other script put inside all the values, you are sure they will be not replaced by zeros.

(PS: upvota and accept answer then, so i can close the post)

Bye!

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

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

Trying to spawn enemies on only one path 1 Answer

How to make an int in an array expand? 0 Answers

Can I create a GameObject Array then fill UI text elements and then somehow use a For Loop to drop an Array of Int values inside those UI text boxes from largest to lowest? 0 Answers

Pull int from string if within square brackets 1 Answer

Need help checking if an item slot is filled 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