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 azmundai · Apr 03, 2013 at 12:42 AM · c#instantiatearray

Instantiate and arrays

I am at a loss here. What I am trying to do is create a level from a number of prefabs. This is just the first pass the create 1-5 platforms. I want to build all of the prefabs that get created into an array that I can then check as I add more things to make sure nothing is colliding (I could use code for that too, but for now just want to get the array working :( ). That is what the while Z is for, I want to just move it until I find a location that isn't colliding.

code:

public class CreatePlatforms : MonoBehaviour {

     private int x;
     private int y;
     private bool z;
     private float xCoord;
     private float zCoord;
     public Transform Platform;
     private int count = 0;
     private GameObject[] prefabArray;
     
     // Use this for initialization
     void Start () {
         for(x=0;x<5;x++)
         {
             y = Random.Range(1, 100);
             if(y < (x * 20))
             {
                 Debug.Log(count);
                 prefabArray[count] = Instantiate(Platform, new Vector3(0, 0.5f, 0), Quaternion.identity) as GameObject;
                 Debug.Log("platform added");
                 z = false;
                 while(!z)
                 {
                     xCoord = Random.Range(-25.0f, 25.0f);
                     zCoord = Random.Range(-25.0f, 25.0f);
                     prefabArray[count].transform.position = new Vector3(xCoord, 0.5f, zCoord);
                     z = true;
                 }                
                 count++;
             }
         }
             
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }

error :

 NullReferenceException: Object reference not set to an instance of an object
 (wrapper stelemref) object:stelemref (object,intptr,object)
 CreatePlatforms.Start () (at Assets/CreatePlatforms.cs:23)


Any help is much appreciated.

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

2 Replies

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

Answer by whebert · Apr 03, 2013 at 12:58 AM

You need to instantiate your prefab array first, before you try to assign anything inside it.

 private GameObject[] prefabArray = new GameObject[5];
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 azmundai · Apr 03, 2013 at 01:07 AM 0
Share

Thanks, now i get the same error on line 25. prefabArray[count].transform.position = new Vector3(xCoord, 0.5f, zCoord);

avatar image whebert · Apr 03, 2013 at 01:13 AM 0
Share

Hmm. You probably need to make your "Platform" a GameObject ins$$anonymous$$d of Transform.

avatar image azmundai · Apr 03, 2013 at 01:15 AM 0
Share

idk how you guys know all of this stuff >.< but that worked!! thanks!!

avatar image
1

Answer by robertbu · Apr 03, 2013 at 01:03 AM

You are not allocating any space for the array. You can do something like:

 private GameObject[] prefabArray = new GameObject[10];

In addition given the way you are using it, make Platform a GameObject (and lower case):

 public GameObject platform;

By convention variable names are lower case, classes are upper case. The compiler does not care, but it makes it more difficult for other people to read your code if you use upper case variables.

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

11 People are following this question.

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

Related Questions

Prefabs instantiated from an array are keeping their public int value 1 Answer

Finding the Sum of Values of Multiple GameObjects in an Array + Variable Sized arrays 0 Answers

Randomly instantiate objects from array without choosing the same item twice. 2 Answers

An Instantiated object to array problem 2 Answers

How to put gameObjects to the list? 4 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