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 DM-Games · Jan 18, 2015 at 10:11 PM · instantiatemultiplayerphotonblock

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;
     }
 
 }
 
Comment
Add comment · Show 3
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
avatar image nightowl79a · Jan 20, 2015 at 02:25 PM 0
Share

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?

avatar image tobiass · Jan 20, 2015 at 03:55 PM 0
Share

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.

avatar image DM-Games · Jan 21, 2015 at 06:38 AM 0
Share

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);
             }
         }
     }
 }
 

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by SaraCecilia · Jan 20, 2015 at 01:25 PM

http://answers.unity3d.com/questions/topics/nullreferenceexception.html

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


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