• 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 TooLazyToBeOriginal · May 30, 2021 at 07:36 AM · arrays

Array displaying incorrect info when I try to show the array outside of Update loop

Console Log As you can see the values alternate between 0's and actual values :(. Below is shorterned code I use in my program. The class is just monobehaviour.

     int[] BallOrder = new int[5]; // me initializing the variable in the beginning of the program

     // int[] BallOrder = {4,6,3,2,7}; if I do this instead of showing 0's it 
     shows 4,6,3,2,7 when I click the object.
     void Start() {
     CreateBalls(5);
     }

     void CreateBalls(int HowMany) {
     for (int i = 0; i < HowMany; i++)  {
     BallClone = Instantiate(Ball);
     BallPic = BallClone.GetComponent<SpriteRenderer>();
     // give each ball a random image, each image is a number
     RandImg(); 
     // just using i for testing purposes.        
     BallOrder[i] = i;
     // array should just be 0,1,2,3,4,
 }
 }

   void ShowList() 
   {
 print(string.Join(" ", BallOrder));
 }

   void Update()
   {
 // here it shows the array correctly 0,1,2,3,4
 ShowList();
   }
    void OnMouseDown() {
    string BallNum = ConvertSprite(GetComponent<SpriteRenderer>().sprite);
    int test = int.Parse(BallNum);
    // here when I show list it just shows 0,0,0,0,0 or 4,6,3,2,7 depending on how I declared it
    ShowList(); 
    Destroy(gameObject);
     }


console.png (16.3 kB)
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 CodesCove · May 30, 2021 at 04:53 PM 0
Share

Here are few things to check: Your console shows 6 values printed and your array has size of 5. Are you sure you have the right monobehaviour attached to GO (most probably not)? Also what are the other numbers than the zero (2 20 ..) in the console, how are these the actual ones as you say?? Also are you sure the mono is attached only to one GO and only once? You can add the object name (or instance id) to you print to check this. Also don't use print when debugging but use Debug.Log(..). You also destroy the gameobject this script is attached in onmousedown.

Well anyway I think the console print you are showing is not from the script you posted here and that is the problem (at least one of them)..

avatar image TooLazyToBeOriginal · May 30, 2021 at 06:07 PM 0
Share
  1. What do you mean by "right monobehaviour attached to GO"? I'm new to unity so not sure what that is.

  2. I picked randoms number for when I ran the console log and choose 0,1,2,3,4,5 when I was testing out. so despite the picture showing random numbers, it was because I used a random number generator instead. My bad

  3. I say actual ones because each ball has an image with a number on it and the image name is a number so I can easily check what's correct or not correct.

  4. I checked and this script is only attached to one game object, I make duplicates and wonder if that is the issue.

  5. how do I add object name (or instance id) to the Debug.Log?

  6. I removed destroy and if I click again it still shows 0's

Show more comments
avatar image TooLazyToBeOriginal · May 30, 2021 at 07:35 PM 0
Share

So I replaced the print with the line u specified and my instance id number is different. Is it bad that each ball has its own instance or is it fine (probably not but just to clarify). The parent gameobject is displaying the correct one but not any of the child ones. aka the clones. So the new values are only added to the parent gameobject (where I have the script attached to) and not to any of the clones. So I believe I have to make the clones first then add to the list.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

121 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

Related Questions

Type function(): Object[] does not support Slicing 1 Answer

Convert BuiltIn array to an Generic List? 1 Answer

snake problem 0 Answers

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

Unity c# indexoutofrangeexception: array index is out of range 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