Hello to everyone I made two scripts one should hit collider in one side of the room, what will activate animation on the object it another side of the room. Raycast works good but when hit Input button it give a Nullreference tried to google but it didn’t helped.
Can someone help please.
Here it is RayCast Script
private float CeilingDistance = 5f;
public phoneAnimationScript myphoneAnimationScript;
public phoneAnimationScript myCharacterAnimationScript;
public CeilingDoorAnimCtrl CeilingDoorsCtrl;
Camera CameraMain;
public void Start()
{
CameraMain = GetComponent<Camera>();
}
public void Update()
{
RaycastHit CeilingButton;
Ray ButtonRay = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
if (Physics.Raycast(ButtonRay, out CeilingButton, CeilingDistance))
{
if (CeilingButton.transform.gameObject.collider.tag == "CellTrigger")
{
if (Input.GetButtonDown("actButton"))
{
CeilingDoorAnimCtrl cas = GetComponent<CeilingDoorAnimCtrl>();
CeilingButton.collider.gameObject.GetComponent<CeilingDoorAnimCtrl>().OpenDoors();
}
Debug.Log("hitCollider");
}
}
}
}
And the Animation Code
public class CeilingDoorAnimCtrl : MonoBehaviour {
private Animator CeilingAnimator;
static int OpenState = Animator.StringToHash("Base Layer.OpenCeilingDoorAnim");
static int CloseState = Animator.StringToHash("Base Layer.CloseCeilingDoorAnim");
// Use this for initialization
void Start () {
CeilingAnimator = GetComponent<Animator>();
}
public void OpenDoors()
{
CeilingAnimator.SetBool("Open", true);
}
public void ClosedDoors()
{
CeilingAnimator.SetBool("Close", true);
}
}
I’d also say try to stick you a consistent naming scheme. Some of you’re classes/types are capitalised, some aren’t, some variables are capitalised, some aren’t. Makes it really hard to read, you’ll also probably find yourself trying to access a class when you meant to access a instantiated variable.
Sorry I didn’t describe it well. I try to do it like this.
Player → collider(TriggerObject) → Door(Object with a animation script) “collider and Door are a separate objects. And somewhere between them I got a null reference alsow collider is just a trigger it didn’t have any scripts (should I add ones?)
And here is the error.
NullReferenceException: Object reference not set to an instance of an object
and you’ve missed off however many lines of “using xxxx” from the top of your code chunk so we still cannot see what line the error it referring to… anyway, if it’s line 69 in the chunk you’ve just pasted (2 usings and a blank space missing), it would appear the thing that was hit doesn’t have a “CeilingDoorAnimCtrl” script attached to it.
on a design note, you appear to be doing things “backwards”, 4 raycasts each frame to check for each thing, all of which only do something when a button is pushed
check for the button push,
raycast to see what was hit,
do something based on the thing hit.
would make a lot more sense and be event driven (the button push) rather than repeating every frame. Raycasts might not be the biggest sin per frame but it’s still a odd approach
edit: and if you want to get super snazzy you could hand off the “do something” to the object that was hit so it’s
check for button push,
raycast to hit,
tell hit to “do it’s thing”
rather than doing everything in this script “remotely”
I made it thank to give me an idea. I just create a relocate script for a separate object.
Have to use 3 scripts now but it work.
Raycast
RaycastHit CeilingButton;
Ray ButtonRay = GetComponent<Camera>().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f));
if (Physics.Raycast(ButtonRay, out CeilingButton, CeilingDistance))
{
if (CeilingButton.transform.gameObject.collider.tag == "CellTrigger")
{
if (Input.GetButtonDown("actButton"))
{
RelocateScript pas = GetComponent<RelocateScript>();
CeilingButton.collider.GetComponent<RelocateScript>().Relocate();
}
Debug.Log("hitCollider");
}
}
}
}
Then Relocate Script on a seperate collider
public class RelocateScript : MonoBehaviour {
public CeilingDoorAnimCtrl CellDoorAnim;
public void Relocate ()
{
CeilingDoorAnimCtrl pas = GetComponent<CeilingDoorAnimCtrl>();
CellDoorAnim.GetComponent<CeilingDoorAnimCtrl>().OpenDoors();
}
}
And Then Animation
public class CeilingDoorAnimCtrl : MonoBehaviour {
private Animator CeilingAnimator;
static int OpenState = Animator.StringToHash("Base Layer.OpenCeilingDoorAnim");
static int CloseState = Animator.StringToHash("Base Layer.CloseCeilingDoorAnim");
// Use this for initialization
void Start () {
CeilingAnimator = GetComponent<Animator>();
}
public void OpenDoors()
{
CeilingAnimator.SetBool("Open", true);
}
public void ClosedDoors()
{
CeilingAnimator.SetBool("Close", true);
}
}