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 RealBigHindi · Sep 20, 2014 at 06:02 PM · javascriptinstantiatearray

Array won't be filled by Instantiate

Hi guys (and girls ;)),

I just started using Untity and atm I'm fooling around a bit. In this case I'm trying to instantiate grassPrefabs and manage them in an array so I can always check if the player is too far from the grass and it shouldn't be rendered to save ressources, but somehow the array I'm creating stays empty.

I guess the important part is the very last line.

Ty for your help,

RBH

 #pragma strict
 
 //Normales Gras
 public var Gras1Prefab : Transform;
 var grasArray : GameObject[];
 
 //Spieler erstellen
 public var PlayerPrefab : Transform;
 var iPlayerPrefab : GameObject;
 
 //Level innitiieren.
 var newLevel = 1;
 public static var GrasColor:Color = new Color(0f, 1f, 0f, 1f);
 var mapX : int;
 var mapZ : int;
 
 function Start () {
 
 }
 
 function Update () {
     
     //Lade neue Level
 
     if (newLevel==1){
         newLevel=0;
         iPlayerPrefab = GameObject.Find("PlayerPrefab");
         var filePath = Application.dataPath + "/Resources/Textures/Level/Level1.png";
             if (System.IO.File.Exists(filePath)){
                var bytes = System.IO.File.ReadAllBytes(filePath);
                var tex = new Texture2D(1, 1);
                tex.LoadImage(bytes);
                
                iPlayerPrefab.transform.position = Vector3 (5,5,5);
                
                transform.localScale += Vector3(tex.width ,0,tex.height);
                transform.localPosition += Vector3(tex.width/2,0,tex.height/2);
                mapX=tex.width;
                mapZ=tex.height;
                
                //Erstellung von "Normales Gras
                grasArray = Array(tex.width*tex.height);    
                for(var x=0; x<tex.width; x++){
                    for(var z=0;z<tex.height; z++){
                       var pixel:Color = tex.GetPixel(x, z);
                        if (pixel==GrasColor){    //Normales Gras
                            grasArray[x+(tex.width*z)] = Instantiate (Gras1Prefab, Vector3(x,1,z), Gras1Prefab.transform.rotation) as GameObject;
                        }
                    }
                }
         }
     }
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 fafase · Sep 20, 2014 at 06:06 PM

I am not so used to UnityScript anymore but I would think you need to instantiate your array. You have a reference for an array of type GameObject but you never tell how many spot it should have:

var grasArray = new GameObject[size];

The big advantage of UnityScript over C# is that it does not seem to let you know you are doing wrong and then you are up for some long hours while C# would have told you right on that this array is not existing.

Comment
Add comment · Show 1 · 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 RealBigHindi · Sep 21, 2014 at 07:30 AM 0
Share

That's what i wanted to do in Line 42:

grasArray = Array(tex.width*tex.height);

but even if I change it to your solution, there is no difference. Also I looked the array up with both options, it always had the right size (in this case 1024 because the mapsize ist 32*32), but is always just empty.

Well, I made a workaround by not managing all gras together but every by itself by adding a new script. Not what i wished for, but also works.

Ty :)

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

Avoiding instantiating two things in same location? 1 Answer

How to use an array to set path by instantiating nodes? 1 Answer

Trying to instantiate random enemy prefab from array 3 Answers

[--FIXED--][UPDATE#5][VIDEO]Instantiating problem(OLD TITLE:How do I change the static variable of one object instead of all instances 3 Answers

Array variable assigned to instantiated object changes in all instantiated objects? 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