• 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 Rokzero · Oct 04, 2014 at 10:10 PM · javascriptgameobjectarrays

JavaScript 3 Arrays questions

Hello guys, I want to start with Unity java script but I can`t understand it even after 2 years Action Script 3 coding. Can you help me? I want to create a 2d array of game objects, so I wrote this:

 public var grass:Transform;
 public var gameobj : GameObject[];
 
 function Start () {
 
     for (var y = 0.0; y < 10; y++) {
             for (var x = 0.0; x < 10; x++) {
                 var sample = Mathf.PerlinNoise(1, 1);
                 Instantiate(grass);
                 grass.transform.position = Vector3 (x, y, 0);
                 gameobj.Add(grass);
                 //Debug.Log();
             }
     }
     
 }
 
 function Update () {
 
 }

But it will not work, I don`t understand why. How do you correctly create game objects from library? How do you push them in arrays?

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
0

Answer by robertbu · Oct 04, 2014 at 10:12 PM

You are stuffing the prefab into the array, not the game object you create. Try this:

 import System.Collections.Generic;

 public var grass:Transform;
 private var myList : List.<GameObject> = new List.<GameObject>();
  
 function Start () {
  
     for (var y = 0.0; y < 10; y++) {
             for (var x = 0.0; x < 10; x++) {
                 var sample = Mathf.PerlinNoise(1, 1);
                 var go : GameObject = Instantiate(grass);
                 go.transform.position = Vector3 (x, y, 0);
                 gameobj.Add(go);
             }
     }
  
 }

Note that your PerlinNoise() will always return the same value the way it is currently being called.

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 Rokzero · Oct 04, 2014 at 10:22 PM 0
Share

Hi, thank you for your answer. But I`m still getting errors here. CreateWorld.js(25,25): Error BCE0019: 'Add' is not a member of 'UnityEngine.GameObject[,]'. (BCE0019) (Assembly-UnityScript)

avatar image robertbu · Oct 04, 2014 at 10:26 PM 0
Share

You are right. Built-in arrays don't have an Add() method. You need to use a generic list. Here is a quick, untested rewrite. $$anonymous$$ore about the various collections here:

http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use%3F

avatar image Rokzero · Oct 04, 2014 at 10:33 PM 0
Share

Hmm, it is too difficult to understand it now, I think I need to start with simple things... Do you know any good tutorials for beginners like me?

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

JS file deleting multiple game objects 0 Answers

Changing sprites using Sprite.Create does not take effect? 0 Answers

How to do transform.LookAt() from a different part of object? 1 Answer

Index out of bounds even tho it shouldnt be [Javascript] 1 Answer

[UNSOLVED] Get a GameObject width and height in pixels (JS) 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges