- Home /
NullReference
Here is my code
using UnityEngine;
using System.Collections;
public class SolarSystemCreate : Photon.MonoBehaviour {
public GameObject GasGiants;
public GameObject Planets;
float PlanetX;
float PlanetY;
float PlanetZ;
float GasGiantX;
float GasGiantY;
float GasGiantZ;
int PlanetCount;
int GasGiantCount;
int IsGasGiant;
int IsPlanet;
// Use this for initialization
void Start () {
IsGasGiant = Random.Range (1,30);
IsPlanet = Random.Range (1,10);
PlanetCount = Random.Range (1,20);
GasGiantCount = Random.Range (1,3);
int i = 0;
int h = 0;
// If IsPlanet is equal to 1 spawn planets
if(IsPlanet == 1){
while (i < PlanetCount) {
PlanetX = transform.position.x + Random.Range (-20384, 20384);
PlanetY = transform.position.y + Random.Range (-30, 30);
PlanetZ = transform.position.z + Random.Range (-20384, 20384);
GetComponent<PhotonView> ().RPC ("PlanetCreate",PhotonTargets.All);
i++;
}
}
//If IsGasGiant is equal to 1 spawn GasGiants
if (IsGasGiant == 1) {
while(h < GasGiantCount){
GasGiantX = transform.position.x + Random.Range (-20384, 20384);
GasGiantY = transform.position.y + Random.Range (-30, 30);
GasGiantZ = transform.position.z + Random.Range (-20384, 20384);
GetComponent<PhotonView> ().RPC ("GasGiantCreate",PhotonTargets.All);
h++;
}
}
//fin
}
// Update is called once per frame
void Update () {
}
[RPC]
public void PlanetCreate () {
Instantiate(Planets, new Vector3 (PlanetX, PlanetY, PlanetZ), Quaternion.identity);
}
[RPC]
public void GasGiantCreate (){
Instantiate (GasGiants, new Vector3 (GasGiantX, GasGiantY, GasGiantZ), Quaternion.identity);
}
}
What is suppose to happen is that it spawns in the planets and gas giants if the int GasgiantCount and PlanetCount is equal to one. But when it is equal to one it gives the error: NullReferenceException: Object reference not set to an instance of an object SolarSystemCreate.Start() (at Assets/Engine/SolarStstem/SolarSystemCreate.cs:31) I read that NullReferenceException is usually when something is getting a null (or nothing) and it needs a alternative code for when it does. But how can I get a alternative null code? By the way the errors are on line 31 and 41. Thanks for the help!
Answer by tyrike thompson · Jul 30, 2014 at 01:01 AM
You're getting a "NullReferenceException" on lines 41 and 31. Maybe it's because the component you are trying to receive (PhotonView) is not attached to the gameObject this script is attached to.
Answer by Bunny83 · Jul 30, 2014 at 01:02 AM
Well, the error is pretty straight forward: You try to access the PhotonView component which should be attached to the same object as this script is attached to, but there isn't such a component! That's why you get a null-ref-error when you try to execute the "RPC" method of that component which doesn't exist.
Your answer
Follow this Question
Related Questions
Null Ref Exeption 1 Answer
NullReferenceException: Object reference not set to an instance of an object ProgressBar.Start () 2 Answers
NullReferenceException: Object reference not set to an instance of an object... 0 Answers
Best practice handling deleted object refences during SceneManager.LoadScene 0 Answers
NullReferenceException with UNET void 0 Answers