Greetings. I have this checkPlayer script that has a Physics.Linecast going from the camera to my character object and a fader script that checks if the checkPlayer script has hit it(object attached to this script) and fades itself when it does and returns its original alpha when it doesn’t.
checkPlayer script:
using UnityEngine;
using System.Collections;
public class checkPlayer : MonoBehaviour {
public GameObject player;
public RaycastHit hit;
void FixedUpdate ()
{
Physics.Linecast (transform.position, player.transform.position,out hit);
}
}
fader script:
using UnityEngine;
using System.Collections;
public class fader : MonoBehaviour {
public checkPlayer check;
Renderer[] renderers;
void Start(){
renderers = GetComponentsInChildren<Renderer> ();
}
void Update(){
if (check.hit.collider.gameObject.tag == "level") {
foreach (var r in renderers) {
Color color = r.GetComponent<Renderer>().material.color;
color.a = 0.2f;
r.GetComponent<Renderer>().material.color = color;
}
} else {
foreach (var r in renderers) {
Color color = r.GetComponent<Renderer>().material.color;
color.a = 1.0f;
r.GetComponent<Renderer>().material.color = color;
}
}
}
}
Now the code works. Problem is that everytime I make my character fall from the platform and spawn/moves its transform.position to my returnpoint object. Unity throws me a
Object reference not set to an
instance of an object fader.Update ()
(at Assets/scripts/fader.cs:13)
Which leads to the Block:
if(check.hit.collider.gameObject.tag == “level”)
I don’t have any idea why it does that everytime it spawns… I’m suspecting its some memory issues from the fader script or something. Anyways, if anyone could help me with this would be a delight!! I’m considering an alternative to an object fader other than the one I have so if anyone has suggestions feel free to say so! Thank you in advance!!