Greetings
I am working on a game where I am having a little difficulty.
I have a game object at the foot of my player and when I ray Cast to it, if it’s touching a certain platform I want to call a separate and invoke a method.
Now this is the script im calling and it is attached to the platform
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlatformFall : MonoBehaviour
{
public float fallDelay = 0.5f;
Animator anim;
private Rigidbody2D rb2d;
void Awake()
{
anim = GetComponent<Animator>();
rb2d = GetComponent<Rigidbody2D>();
}
void OnCollisionEnter2D(Collision2D other)
{
Debug.Log(other.gameObject.tag);
GameObject childObject = other.collider.gameObject;
Debug.Log(childObject);
if (other.gameObject.CompareTag("Feet"))
{
anim.SetTrigger("PlatformShake");
Invoke("Fall", fallDelay);
// destroy the Log
DestroyObject(this.gameObject,4);
}
}
public void startFall()
{
anim.SetTrigger("PlatformShake");
Invoke("Fall", fallDelay);
// destroy the Log
DestroyObject(this.gameObject, 4);
}
void Fall()
{
rb2d.isKinematic = false;
rb2d.mass = 15;
}
}
Then in my other script which is attached to my character .
public class CharacterController2D : MonoBehaviour {
//JummpRay Cast
public PlatformFall platfall;
// LayerMask to determine what is considered ground for the player
public LayerMask whatIsGround;
public LayerMask WhatIsFallingPlatform;
// Transform just below feet for checking if player is grounded
public Transform groundCheck;
....
...
Update(){
// Ray Casting to Fallingplatform
isFallingPlatform = Physics2D.Linecast(_transform.position, groundCheck.position, WhatIsFallingPlatform);
if (isFallingPlatform)
{
Debug.Log("Here");
platfall.startFall();
}
Debug.Log(isFallingPlatform);
The Problem
My problem is, when my player touches the “platform” where message “Here” appears in the debug log along with the error"
UnassignedReferenceException: The variable anim of PlatformFall has not been assigned.
You probably need to assign the anim variable of the PlatformFall script in the inspector.
and if I comment out anim.SetTrigger I get
UnassignedReferenceException: The variable rb2d of PlatformFall has not been assigned.
You probably need to assign the rb2d variable of the PlatformFall script in the inspector.
I am not entirely sure why I am getting this error because my animator and rigidbody2d is set in the inspector of the fallingplatform and when I use the OnCollisionEnter2D it works fine. but calling platfall.startFall() from my other script is what’s giving the error.
Any help would be appreciated.
Thank you