Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Beta_Artist · Oct 03, 2011 at 11:46 PM · javascriptarrayspawn

Making an Array of Spawnpoints...

Looking to make a working Array of Game Objects that can be used for spawn locations. I made an Array but I'm getting a error:

"NullReferenceException: Object reference not set to an instance of an object"

Which when clicked tell me that there is something wrong with this line of code:

 instance = Instantiate(game_cube, arr[randomPick].position, arr[randomPick].rotation);

Here is my script:

 private var arr : Array;
 
 var game_cube : Rigidbody;
 var cube_count = 0;
 var walker0 : GameObject;
 var walker1 : GameObject;
 var walker2 : GameObject;
 
 
 function Start () {
     arr = new Array[3];
         arr.Push(walker0);
         arr.Push(walker1);
         arr.Push(walker2);
 }
 InvokeRepeating("LaunchProjectile", 2, 3);
 function Update() {
     if (cube_count>= 10) {
         CancelInvoke();
     }
 
 }
 
 function LaunchProjectile () {
     var randomPick : int = Random.Range(0,2);
         
     instance = Instantiate(game_cube, arr[randomPick].position, arr[randomPick].rotation);
     instance.velocity = Vector3.zero;
     cube_count = cube_count +1;
 
 }
Comment
Add comment · Show 2
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 vxssmatty · Oct 04, 2011 at 12:28 AM 0
Share

Why did you repost this Question? did you even look at your old post?

http://answers.unity3d.com/questions/172932/multiple-spawn-points-using-array.html

I admire your effort to trying multiple solutions to your scenario, means you are actually researching and finding things out for yourself. Cu dose for that.

avatar image DaveA · Oct 04, 2011 at 12:36 AM 1
Share

Yes I think the answer is on the original thread. You needed Start or Awake to get it going. It should have given you compile errors.

1 Reply

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

Answer by aldonaletto · Oct 03, 2011 at 11:59 PM

You've pushed GameObjects in your array, but you're trying to read position and rotation, which are properties of Transform, not GameObject. You can change the walker types to Transform (the Inspector will store the correct type). Maybe you need also assign the array element to a Transform variable prior to use it:

    var sPoint: Transform = arr[randomPick];
    instance = Instantiate(game_cube, sPoint.position, sPoint.rotation);
    ...
Make sure that the 3 walker variables and the game_cube are not empty, or this error will haunt you again.
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 Beta_Artist · Oct 04, 2011 at 01:14 AM 0
Share

Ok, I made those changes and for some reason I'm still getting the same error message. I dragged the objects onto the game_cube and all the walkers. Could it be something wrong with my Array? Did I fill it out correctly?

Forgive me, I'm an artist, this is the first time I've tried to program anything.

avatar image vxssmatty · Oct 04, 2011 at 02:36 AM 0
Share

You still have other problems in your script, refer to your other question.

http://answers.unity3d.com/questions/172932/multiple-spawn-points-using-array.html

avatar image aldonaletto · Oct 04, 2011 at 02:45 AM 0
Share

Reading your question again, I noticed that you're creating the array arr with 3 elements, but when you use arr.Push the new element is added to the array, so you end with 6 elements - and the first 3 are empty. Change the first line of Start to:

function Start(){
    arr = new Array(); // create an empty array
    ...

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Multiple spawn points using Array... 1 Answer

Calling random functions 4 Answers

Resizing arrays, and what is the default array type anyway? 3 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