- Home /
what has gone wrong with this script
i have a problem with this code i got from a you tube tutorial i copied the code from the linked repository and renamed one script from worldgen to voxelterrain but i get this error
NullReferenceException: Object reference not set to an instance of an object Player.Start () (at Assets/scripts/Player.cs:10)
i looked at the code and the tutorials to try to figure out what was wrong but the youtuber does not actualy make anything called World in his videos ill put the voxelterrain script up for reference as well as the offending script
player script(offending)
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
VoxelTerrain world;
void Start()
{
world = (VoxelTerrain)GameObject.Find("World").GetComponent(typeof(VoxelTerrain));
}
void Update()
{
if(Input.GetMouseButton(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit))
{
BreakBlock(BlockAtPoint(hit.point));
}
}
}
Voxel BlockAtPoint(Vector3 point)
{
Debug.Log("X: " + FloatToInt(point.x) + " Y: " + FloatToInt(point.y) + " Z: " + FloatToInt(point.z));
return world.chunk.voxels[FloatToInt(point.x),FloatToInt(point.y),FloatToInt(point.z)];
}
void BreakBlock(Voxel block)
{
block.type = 0;
}
int FloatToInt(float value)
{
float dec = (int)value - value;
if(dec < 0.5)
{
float num = value - dec;
return (int)num;
}
else
{
return (int)value;
}
}
}
Voxelterrain script
using UnityEngine;
using System.Collections;
public class VoxelTerrain : MonoBehaviour
{
Chunk CHNK;
public Chunk chunk {
get {return CHNK;}
set {CHNK = value;}
}
public string test = "Hello World!";
void Start()
{
chunk = new Chunk();
}
void Update()
{
chunk.Update();
}
}
yes i made an empty called world but it still gives the same error
Answer by bubzy · Oct 04, 2014 at 01:38 AM
you may need to
VoxelTerrain world = new VoxelTerrain();
and of course if any constructors are required, use those too.
ok so i figured out my problem my file structure was wrong but your answer was really helpful so i chose your one
Your answer

Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Cube World Terrain generation 2 Answers
Multiple Cars not working 1 Answer
Using noise to generate Cube World like terrain 0 Answers
How to make a voxel terrain generate all around the start point 1 Answer