using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sign_Wooden_Blank : Interactable
{
public string[] dialogue;
public string name;
AnimationCamera ac;
public override void Interact()
{
ac = GetComponent<AnimationCamera>();
ac.PlaySignAnimation();
}
}
The null is on the ac.PlaySignAnimation(); ac is null. I tried to make ac = new AnimationCamera(); but i'm getting a warning to use GetComponent and then ac is null.
And the AnimationCamera script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimationCamera : MonoBehaviour
{
public Camera animationCamera;
public Camera mainCamera;
Animator _anim;
private void Start()
{
animationCamera.enabled = false;
mainCamera.enabled = true;
_anim = GetComponent<Animator>();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.C))
{
animationCamera.enabled = !animationCamera.enabled;
mainCamera.enabled = !mainCamera.enabled;
if (animationCamera.enabled)
{
_anim.CrossFade("Animation_Sign", 0);
}
else
{
_anim.CrossFade("Animation_Idle", 0);
}
}
}
public void PlaySignAnimation()
{
_anim = GetComponent<Animator>();
_anim.CrossFade("Animation_Sign", 0);
}
}
I want that when there is Interaction and it's calling the function Interact in the first script then play the specific animation in the AnimationCamera.
And if i'm using in the first script ac = new AnimationCamera(); Then in the AnimationCamera script the _anim is null in the PlaySignAnimation function.
I have to explain a bit more:
The first script the Sign_Wooden_Blank is type: Interactable I have another two script for interaction:
WorldInteraction:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WorldInteraction : MonoBehaviour
{
public int speed = 5; // Determines how quickly object moves towards position
public float rotationSpeed = 5f;
public bool autoMove = true;
private Animator _animator;
UnityEngine.AI.NavMeshAgent playerAgent;
private void Start()
{
_animator = GetComponent<Animator>();
_animator.CrossFade("Idle", 0);
playerAgent = GetComponent<UnityEngine.AI.NavMeshAgent>();
}
private void Update()
{
if (Input.GetMouseButtonDown(0) && !UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject())
{
GetInteraction();
}
}
void GetInteraction()
{
Ray interactionRay = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit interactionInfo;
if (Physics.Raycast(interactionRay, out interactionInfo, Mathf.Infinity))
{
GameObject interactedObject = interactionInfo.collider.gameObject;
if (interactedObject.tag == "Interactable Object")
{
interactedObject.GetComponent<Interactable>().MoveToInteraction(playerAgent);
}
else
{
playerAgent.stoppingDistance = 0;
playerAgent.destination = interactionInfo.point;
}
}
}
}
And Interactable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Interactable : MonoBehaviour
{
[HideInInspector]
public UnityEngine.AI.NavMeshAgent playerAgent;
private bool hasInteracted;
public virtual void MoveToInteraction(UnityEngine.AI.NavMeshAgent playerAgent)
{
hasInteracted = false;
this.playerAgent = playerAgent;
playerAgent.stoppingDistance = 1.5f;
playerAgent.destination = this.transform.position;
}
private void Update()
{
if (!hasInteracted && playerAgent != null && !playerAgent.pathPending)
{
if (playerAgent.remainingDistance <= playerAgent.stoppingDistance)
{
Interact();
hasInteracted = true;
}
}
}
public virtual void Interact()
{
Debug.Log("Interacting with base class.");
}
}
The script Sign_Wooden_Blank is attached to object Sign_Wooden_Blank_01 The script AnimationCamera is attached to Camera.
Here is an example of how i used another object with the Interactable:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickupItem : Interactable
{
public override void Interact()
{
Debug.Log("Interacting with Item.");
}
}
The whole idea is to interact with the sign wood. So when i get interacted with it then start the animation clip.
The worldinteraction script is attached to ThirdPersonController. I guess i messed it all up. Not sure how to fix it.