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 pajamajama · Oct 22, 2012 at 01:09 AM · randomrandom.rangerespawnnullreference

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

I'm sure this is a simple fix for many of you. Thank you for any advice you can offer. I'm trying to respawn my enemy to a random set of coords. I've defined the coords in an array and cant get it to work.

I've gotten the enemy to respawn before by just using a random.range of integers, but when using the method below to generate specific coords I get the nullreferenceexception as soon as I shoot the enemy. The enemy also does not respawn.

The line that pulls the exception is "newX = coords[Random.Range(0, coords.Length-1)][0];". I imagine it would do the same for the next line as well.

 function OnTriggerEnter(otherObject: Collider){
 
     if(otherObject.gameObject.tag =="enemy"){
         playerscript.playerScore += 100;
         
         var newY : int;
         var newX : int;
         
         var coords = new Array(new Array(-8,8), new Array(0,8), new Array(8,8), new Array(8,0), new Array(8,-8), new Array(0,-8), new Array(-8,-8), new Array(-8,0));
         
         newX = coords[Random.Range(0, coords.Length-1)][0];
         newY = coords[Random.Range(0, coords.Length-1)][1];
         
     
         otherObject.gameObject.transform.position.x = newX;
         otherObject.gameObject.transform.position.y = newY;
         
         Destroy(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

2 Replies

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

Answer by dannyskim · Oct 22, 2012 at 02:11 AM

Other than what Seth Bergman said, the solution to your Null Reference Exception is most likely because you're getting the Length of a multidimensional array, when in fact you need to use GetLength(), where GetLength(0) is the row length, and GetLength(1) is the column length.

And from judging from what you're 'planning' to do, Seth's solution is not what you're needing anyway, cause that would return any number between -8,7 ( because Random.Range is max exclusive ), whereas it looks like you're trying to retrieve is -8, 0, or 8 as the integers and nothing else.

http://msdn.microsoft.com/en-us/library/system.array.getlength.aspx

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 pajamajama · Jul 18, 2013 at 08:45 PM 0
Share

Thank you very much. Problem solved.

avatar image
0

Answer by Seth-Bergman · Oct 22, 2012 at 01:36 AM

OK, I have NO idea why you are trying to use all these arrays, just to come up with two random ints! just use Random.Range, forget all that other stuff:

 function OnTriggerEnter(otherObject: Collider){
 
     if(otherObject.gameObject.tag =="enemy"){
        playerscript.playerScore += 100;

        var newY : int = Random.Range (-8,8);
        var newX : int = Random.Range (-8,8);
 
        otherObject.transform.position = Vector3(newX,newY,0);
 
        Destroy(gameObject); 
     }

EDIT:

the main point is this is not a very efficient way to do this.. at the very most, one array (8,0,-8), would be all you would need, assuming you only want these values:

        var coords = new Array(-8,0,8);
        var newY : int = coords[Random.Range(0,3)];
        var newX : int = coords[Random.Range(0,3)];
        otherObject.transform.position = Vector3(newX,newY,0);

I also fixed the last line.. Collider inherits transform directly, (but not gameObject either way)

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 dannyskim · Oct 22, 2012 at 05:37 AM 0
Share

I completely agree with you on the efficiency. But where I don't agree with, and which I pointed out, is that you technically did not answer his question.

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

Random.Range Vector3 2 Answers

Generate random no and active that GO using array 1 Answer

How can I randomly create a number and then delete it to stop repeating 3 Answers

How can I Instantiate and do it in a random spot in a random spot? 2 Answers

Teleport to a random height 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