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

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

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

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

Once again, adding GameObjects to an array 1 Answer

How to correctly convert Object to GameObject 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