- Home /
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
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613011414im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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