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 the Jack · Jul 07, 2010 at 03:15 PM · arrayarraysgameobjects

Problems adding GameObjects to a GameObject array (C#)

Hello!

I'm declaring a GameObject array and then trying to add GameObjects to it.

My declaration looks like this:

public GameObject[] OrbitingSatellites = new GameObject[7];

And my code to add objects just isn't working. I feel like this might be close:

OrbitingSatellites[0] = orbital1;

where "orbital1" is the name of the GameObject I want in the array at position 0.

The errors it throws are

CS0120: 'Orbit.OrbitingSatellites': An object reference is required for the nonstatic field, method or property

CS0021: Cannot apply indexing with [] to an expression of type 'object'

Please help me find a solution, thank you.

EDIT: Declaration of orbital1 looks like this:

public GameObject orbital1;

I'm using this as a container for an instanced primitive sphere. In the inspector it checks out as a game object, and it is indeed a sphere that I'm manipulating.

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 Cyclops · Jul 07, 2010 at 04:01 PM 0
Share

@Jack, can you add the code that creates/declares orbital1? Are you sure it's of type GameObject, and not Object - and not null? :) And also not Transform - I still get those mixed up.

avatar image Cyclops · Jul 07, 2010 at 04:20 PM 0
Share

@Jack, on second thought, that would generate a type-conversion error. It sounds like there is something wrong with the array declaration, but I cut/paste it with no problem. Can you show more of the class code where OrbitingSatellites is declared?

avatar image the Jack · Jul 07, 2010 at 04:33 PM 0
Share

@ Cyclops Yeah, cutting and pasting it straight has given me some insight into how it's supposed to work. The problem then isn't in how I'm handling the array, but somewhere else in the code, so I can close this question thanks to your help. $$anonymous$$uch appreciated!

2 Replies

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

Answer by Cyclops · Jul 07, 2010 at 04:30 PM

Here is a sample of the code I used, after cutting/pasting your lines into a script, which worked with no problem:

public class s_foo : MonoBehaviour { public GameObject[] OrbitingSatellites = new GameObject[7]; public GameObject orbital1; // drag-dropped "small explosion" in Editor.

 void Start() {
     OrbitingSatellites[0] = orbital1;
     Debug.Log("os = " + OrbitingSatellites[0].name); // prints "small explosion"
 }

}

What differences, if any, are in your code structure?

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

Answer by the Jack · Jul 07, 2010 at 04:34 PM

This is a correct method for adding game objects to a gameobject array, my problems lie elsewhere.

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

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

No one has followed this question yet.

Related Questions

Distance error: Cannot cast from source type to destination 0 Answers

How to return index of List element? 1 Answer

How many audio clip arrays are supported? 1 Answer

Get all GameObjects by variable value 2 Answers

How to compare GameObjects in an array? 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