Search Unity

Object reference not set to an instance of an object?

Discussion in 'Scripting' started by BubyMB, Jun 6, 2016.

  1. BubyMB

    BubyMB

    Joined:
    Jun 6, 2016
    Posts:
    140
    Hi guys, I came up with this problem. I am using TairaGames' point to click tutorial.
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SendInfo : MonoBehaviour
    5. {
    6.  
    7.  
    8.     void Start()
    9.     {
    10.  
    11.     }
    12.  
    13.  
    14.     void Update()
    15.     {
    16.  
    17.         bool RMB = Input.GetMouseButtonDown(0);
    18.  
    19.         if (RMB)
    20.         {
    21.  
    22.             RaycastHit hit;
    23.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    24.  
    25.             if (Physics.Raycast(ray, out hit) && hit.transform.tag == "Ground")
    26.             {
    27.                 this.GetComponent<PhotonView>().RPC("RecievedMove", PhotonTargets.All, hit.point);
    28.             }
    29.  
    30.  
    31.         }
    32.  
    33.     }
    34. }
    35.  
    36.  
    There error is:
    NullReferenceException: Object reference not set to an instance of an object SendInfo.Update () (At Assets/Scripts/SendInfo.cs:23)
    I looked at like 23, and couldn't find anything wrong with it;
    Code (csharp):
    1.  
    2.   Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    3.  
     
    Last edited: Jan 3, 2017
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Do you have a camera with a tag "main camera"? (I'm pretty sure, that would give an error like "there is no camera tagged main camera")
     
  3. BubyMB

    BubyMB

    Joined:
    Jun 6, 2016
    Posts:
    140
    Ah, that did the trick! thanks!
     
  4. KyleStank

    KyleStank

    Joined:
    Feb 9, 2014
    Posts:
    204
    I don't want to discourage you, but you are still new, I really don't recommend to trying a muktiplayer game. Just try simple games and add more and more to them. I remember when I started, networking just went over my head lol. Just trying to help
     
  5. BubyMB

    BubyMB

    Joined:
    Jun 6, 2016
    Posts:
    140
    I'm not necessarily 'new' to coding it's just I am new to unity. I use Photon for networking which finishes most of the network off, well to my knowledge. This is more of a test project to see what unity can do.
     
  6. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    He meant new to unity too though
     
  7. BubyMB

    BubyMB

    Joined:
    Jun 6, 2016
    Posts:
    140
    Ah okay, sorry for the misunderstanding