- Home /
Photon- block instantiating problem
Hi all, i am trying to instantiate blocks like in minecraft, but in multiplayer using photon. It gives me the error:NullReferenceException: Object reference not set to an instance of an object
and here's my script:
using UnityEngine;
using System.Collections;
public class Build : MonoBehaviour {
RaycastHit hit;
LayerMask buildLayer = 1;
float range = float.PositiveInfinity;
bool hasChecked = false;
void Update ()
{
if(Input.GetKeyDown(KeyCode.B))
{
Debug.Log("Trying to raycast");
Raycast();
}
if(hasChecked == true)
{
GameObject stone = (GameObject)PhotonNetwork.Instantiate("Stone", hit.transform.position + hit.normal, Quaternion.identity, 0);
hasChecked = false;
}
}
void Raycast()
{
Physics.Raycast(transform.position, transform.forward, out hit, range, buildLayer);
hasChecked = true;
}
}
Well probably need a bit more information like when dose it give the the error when u press the b key? Dose it spawn the Stone object at all? Also did you make sure the Stone prefab is in the Resources folder?
The NullReferenceException most likely has a line number at the end of the log entry. Look up this code line and find out what can be null in there and why it could be null.
Fix that.
Ok so i fixed it myself with this code:
using UnityEngine;
using System.Collections;
public class Build : $$anonymous$$onoBehaviour {
Transform retAdd;
Transform retDelete;
RaycastHit hit;
int BuildDistance = 10;
void Start()
{
retAdd = GameObject.Find("RetAdd").transform;
retDelete = GameObject.Find("RetDelete").transform;
}
void Update()
{
if(Physics.Raycast(Camera.main.ScreenPointToRay(new Vector3((Screen.width / 2), (Screen.height / 2), 0)), out hit, BuildDistance))
{
if(hit.transform.tag != "Block")
{
retDelete.renderer.enabled = false;
retAdd.renderer.enabled = true;
retAdd.transform.position = new Vector3(hit.point.x, hit.point.y + 0.5f, hit.point.z);
}
if(hit.transform.tag == "Block")
{
retDelete.transform.position = hit.transform.position;
retDelete.renderer.enabled = true;
retAdd.transform.position = hit.transform.position + hit.normal;
}
if(Input.GetButtonDown("Fire1") && hit.transform.tag != "Player")
{
PhotonNetwork.Instantiate(BlockSelector.selectedBlock, retAdd.transform.position, Quaternion.identity, 0);
}
else if(Input.GetButtonDown("Fire2") && hit.transform.tag == "Block")
{
PhotonNetwork.Destroy(hit.transform.gameObject);
}
}
}
}
Answer by SaraCecilia · Jan 20, 2015 at 01:25 PM
http://answers.unity3d.com/questions/topics/nullreferenceexception.html
Your answer
Follow this Question
Related Questions
Photon Networking Instantiating Problem 0 Answers
trying to catch photonNetwork instantiated object of another user 0 Answers
Instantiating player when creating a new room in photon not working. 0 Answers
PUN 2 : Impossible to applied damage to all player in overlapSphere created by a bomb ? 0 Answers
Photon Instantiate 2 Answers