So today I decided to add the ability to pick up objects you are looking at using a raycast. I was able to properly implement this, but ran into an error when trying to make the objects change their material if they were being selected. What I wanted was for objects to highlight when I was looking at them. But now I have gotten a null reference exception from my outline material script. The error comes from line 26 which has some code that I just barely understand. So what is wrong with my code?
Here is my raycast pickup script (very repetitive, but i am pretty new to coding) just in case it helps, but again, the problem is with the other script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RaycastPickup : MonoBehaviour
{
public float grabDistance;
public Transform face;
public LayerMask objectLayer;
public RaycastHit hitInfo;
public bool isSelecting;
private void Start()
{
isSelecting = false;
}
private void Update()
{
Ray ray = new Ray(face.position, face.forward);
if(Physics.Raycast(ray, out hitInfo, grabDistance, objectLayer))
{
isSelecting = true;
}
else
{
Debug.Log("NOT SELECTING");
isSelecting = false;
}
if(Input.GetKeyDown(KeyCode.E) && Physics.Raycast(ray, out hitInfo, grabDistance, objectLayer))
{
if(hitInfo.collider.gameObject.tag == "Branch" && hitInfo.collider.gameObject.GetComponent<SingleBranchDetection>().touchedGround == true)
{
Destroy(hitInfo.collider.gameObject);
BranchCounter.branchValue++;
HoldingLogic.branchNum++;
}
else if (hitInfo.collider.gameObject.tag == "BranchPile")
{
Destroy(hitInfo.collider.gameObject);
BranchCounter.branchValue += 3;
HoldingLogic.branchNum += 3;
}
else if (hitInfo.collider.gameObject.tag == "Grass" && hitInfo.collider.gameObject.GetComponent<SingleGrassDetection>().touchedGround == true)
{
Destroy(hitInfo.collider.gameObject);
GrassCounter.GrassValue++;
HoldingLogic.grasssNum++;
}
else if (hitInfo.collider.gameObject.tag == "Rock" && hitInfo.collider.gameObject.GetComponent<SingleRockDetection>().touchedGround == true)
{
Destroy(hitInfo.collider.gameObject);
RockCounter.rockValue++;
HoldingLogic.rockNum++;
}
else if (hitInfo.collider.gameObject.tag == "RockPile")
{
Destroy(hitInfo.collider.gameObject);
RockCounter.rockValue += 3;
HoldingLogic.rockNum += 3;
}
}
if(Physics.Raycast(ray, out hitInfo, grabDistance, objectLayer))
{
Debug.DrawLine(ray.origin, hitInfo.point, Color.red);
}
else
{
Debug.DrawLine(ray.origin, ray.origin + ray.direction * grabDistance, Color.green);
}
}
}
And here is my highlight script(it’s called outline, but I will change that to highlight later). I know the parts where I am making the gameobject called “object” just a this.gameobject is a little unnecessary, but I was just trying to figure out what the problem was:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OutliningScript : MonoBehaviour
{
public Material Noramal;
public Material Selected;
MeshRenderer meshRen;
GameObject playerObject;
GameObject Object;
private void Start()
{
playerObject = GameObject.FindGameObjectWithTag("Player");
meshRen = GetComponent<MeshRenderer>();
meshRen.material = Noramal;
Object = this.gameObject;
}
private void Update()
{
if (playerObject.GetComponent<RaycastPickup>().hitInfo.collider.gameObject == Object && playerObject.GetComponent<RaycastPickup>().isSelecting == true)
{
meshRen.material = Selected;
}
else
{
meshRen.material = Noramal;
}
if (playerObject.GetComponent<RaycastPickup>().isSelecting == false)
{
meshRen.material = Noramal;
}
}
}
Please either let me know how to fix this, or give me another way to do this. And if I ask for more specifics, then I am sorry, but I am still new and am trying to learn what everything does