Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 dacuetu · Jan 12, 2012 at 11:14 AM · c#arrayobjectmultidimensional

Multidimensional array of objects in C#

Hi, I'm trying to store the position and angle for each object, for each frame. (It is for a project not related gaming, so don't worry about real-time performance.) I'd like to create a matrix of objects (each object containing the game object ID and position), but since I am new both to Unity and C#, I cannot get it to work. Based with the examples I wrote it like this:

 [System.Serializable]
 class ObjPosInfo
 {
     public string Obj_ID;
     public float Pos_x;
     public float Pos_y;
     public float Angle;
     
     public ObjPosInfo(string Obj_ID, float Pos_x, float Pos_y, float Angle)
     {
         this.Obj_ID = Obj_ID;
         this.Pos_x = Pos_x;
         this.Pos_y = Pos_y;
         this.Angle = Angle;
     }
 }

 void Start () {
     ObjPosInfo[,] frameArray = new ObjPosInfo [numberOfFrames, GameObject.FindGameObjectsWithTag("box")];
 }

When I try to execute, this error appears: Assets/test_script.cs(41,87): error CS0029: Cannot implicitly convert type UnityEngine.GameObject[]' to int'. What am I doing wrong?

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 dacuetu · Jan 12, 2012 at 11:23 AM 0
Share

And how do I access later this frameArray from other parts of the code?

avatar image syclamoth · Jan 12, 2012 at 11:23 AM 0
Share

Well, that's a little more complicated. I'll edit my post for you.

avatar image syclamoth · Jan 12, 2012 at 11:30 AM 0
Share

How much program$$anonymous$$g experience do you have? What languages are you familiar with?

1 Reply

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

Answer by syclamoth · Jan 12, 2012 at 11:23 AM

It's not a big problem- all you forgot to do was put the .Length at the end of the array!

When you declare a multidimensional array, you need two integers, for the two dimensions. However,

GameObject.GindGameObjectsWithTag(string);

returns an array of gameObjects, not an int! Of course, obviously you just want the length of the array (assuming you're going to populate your array later), so all you need to do is change that line to

ObjPosInfo[,] frameArray = new ObjPosInfo [numberOfFrames, GameObject.FindGameObjectsWithTag("box").Length];

Of course, if you want to be able to see this new array from other parts of your code, you need to define it in a shared scope. When you write a method, like so-

public void foo(Bar bar)
{
    // stuff happens here
}

any variables declared inside the curly brackets cannot be seen outside of that function. They are created and destroyed entirely within the scope of those brackets!

If you want to use a variable over several functions, you need to make it a class member.

Of course, if you declare the variable at the top of your class, before any functions, you can't create it on the same line that it is declared, because GameObject.FindGameObjectsWithTag doesn't really mean anything at compile-time. Instead, you should create it like this-

public class ThisIsYourClass : MonoBehaviour { ObjPosInfo[,] frameArray;

 void Awake()
 {
     frameArray = new ObjPosInfo [numberOfFrames, GameObject.FindGameObjectsWithTag("box").Length];
 }

}

Then, whenever you want to access this array from another function, just use

frameArray[number, otherNumber]

to get a member, or just 'frameArray' if there's a function you want to do on the array itself.

Just out of interest, is this a time-travel game?

Comment
Add comment · Show 3 · 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 dacuetu · Jan 12, 2012 at 11:49 AM 0
Share

I am mostly familiar with C, so objects are really a step forward. I think I will manage to do it with your lines. Thanks for the fast and helpful answer!

No, not a game at all, just an university project, but I like the idea of time-travel games :)

avatar image syclamoth · Jan 12, 2012 at 12:21 PM 0
Share

Well, the time-travel thing is because some friends of $$anonymous$$e are making a time-travel game, and part of it works by keeping track of an object's position at every single point in time, through an array of 'frames', much like yours.

avatar image Ra5ta · Mar 26, 2014 at 08:23 AM 0
Share

this was helpfull!! thx

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Object reference not set to an instance of an object 1 Answer

How to properly create a 2 dimensional array of an object. [C#] 1 Answer

Multiple Cars not working 1 Answer

Instantiating objects from a class? (C#) 1 Answer

[SOLVED]Object selection scrip doesn't work. 2 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