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 SomeRandomGuy · Oct 15, 2011 at 10:29 PM · terrainnullreferenceexception

SampleHeight, Object refference not set to an instance of an object error

Hi, I made this script which would randomly spawn a game object onto my terrain, however I can't seem to get the code for getting the correct height right. Here's my code:

 var MassMobs : GameObject;
 var amount = 100;
 var once = false;
 function Update () 
 {
     if (!once)
     {
         for (var i : int = 0; i < amount; i++)
         {
             Instantiate(MassMobs, Vector3(Random.Range(-700,700), Terrain.activeTerrain.SampleHeight(MassMobs.position), Random.Range(-700,700)), Quaternion.identity);
         }
         once = true;
     }
 }

I've looked around and found that I'd need to use the SampleHeight thing to see what the height of the terrain is at those points, however this code gives me the following error:

NullReferenceException: Object reference not set to an instance of an object SpawnSetAmountOnTerrain.Update () (at Assets/Standard Assets/Scripts/MobSpawnScript/SpawnSetAmountOnTerrain.js:10)

So bassically I would guess the problem is in the "MassMobs" I used for the transform, but I can't figure out why it should not work. Any idea's?

Thanks in advance!:D

EDIT: I've been trying some things out, and I think this is basically how I should do it:

 var MassMobs : GameObject;
 var amount = 100;
 var once = false;
 var YCoord = Terrain.activeTerrain.SampleHeight(MassMobs.position);
 function Update () 
 {
     if (!once)
     {
         for (var i : int = 0; i < amount; i++)
         {
             Instantiate(MassMobs, Vector3(Random.Range(-600,600), YCoord, Random.Range(-600,600)), Quaternion.identity);
         }
         once = true;
     }
 }

However, this gives the same error as "MassMobs" is not placed into the scene before it is being instanced. So what I need is a way to determine the height of the terrain at the MassMobs objects position as they are being spawned.

Or perhaps set the height after spawning inside the game object itself, and execute the other scripts on it only after it has determined the height. Any kind of help would be appreciated!:D

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
Best Answer

Answer by SomeRandomGuy · Oct 16, 2011 at 11:12 AM

OK!

The following worked out just fine:

 var MassMobs : GameObject;
 var amount = 100;
 var once = false;
 
 function Update () 
 {
     if (!once)
     {
         for (var i : int = 0; i < amount; i++)
         {
             Instantiate(MassMobs, Vector3(Random.Range(-600,600), 100, Random.Range(-600,600)), Quaternion.identity);
         }
         once = true;
     }
 }

With this script attached to my MassSpawner object, and My spawnable object as the MassMobs variable. I used the following script to make sure the spawnable object would set its height to the Terrains height:

 function Update () {
 var YCoord = Terrain.activeTerrain.SampleHeight(transform.position);
 transform.position = new Vector3(transform.position.x, YCoord,transform.position.z);
 }

After attaching this script to my spawnable object it worked fine. Yay!:D

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

2 People are following this question.

avatar image avatar image

Related Questions

Why is function "SampleHeight" not available 0 Answers

Best way to check which terrain im over? 1 Answer

Character height issue 0 Answers

Problem with Terrain.SampleHeight 0 Answers

Detect closest reachable point of object 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