-2

Having this error

I have tried many options to pass the reference to null such as:

But it didn't work. I know it's a simple error, but I'm new in coding. A little help will be appreciated a lot.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class Paintable : MonoBehaviour {

    public GameObject Brush;
    public float BrushSize = 0.1f;
    public RenderTexture RTexture;

    void Update () {
        if (Input.GetMouseButton(0))
        {
            // error is in this line 
            var Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if(Physics.Raycast(Ray, out hit))
            {
                var go = Instantiate(Brush, hit.point + Vector3.up * 0.1f, Quaternion.identity, transform);
                go.transform.localScale = Vector3.one * BrushSize;
            }
        }
    }

NullReferenceException: Object reference not set to an instance of an object Paintable.Update () (at Assets/Datafiles/Scripts/Paintable.cs:23)

It is giving me error when I start a play mode in unity to draw on a plane with a brush.

Luthfi
  • 478
  • 1
  • 3
  • 16
  • 1
    Possible duplicate of [Camera.main null reference exception](https://stackoverflow.com/questions/52242441/camera-main-null-reference-exception) – Hellium Aug 17 '19 at 05:17

1 Answers1

1

The most likely problem is that there is no camera with the tag of MainCamera. You'll just have to check your Camera object(s) in the editor and change their tag to MainCamera using the dropdown.

Source: https://answers.unity.com/questions/462964/nullreferenceexception-in-cameramainscreenpointtor.html

Axiumin_
  • 2,107
  • 2
  • 15
  • 24