0
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.

Editor

Daniel Halfoni
  • 487
  • 10
  • 30
  • 1
    where is `Sign_Wooden_Blank.cs` attached to? – Hristo May 08 '17 at 11:38
  • Remember that you usually attach scripts to a `GameObject`. `GetComponent` basically asks that parent `GameObject` for another script that was attached to it. So make sure that `Sign_Wooden_Blank`, `AnimationCamera` and `Animator` are all attached to the same `GameObject`. – Botz3000 May 08 '17 at 11:51
  • @Hristo It's attached to object Sign_Wooden_Blank_01 – Daniel Halfoni May 08 '17 at 11:51
  • 1
    Check the duplicated answer. You find the GameObject then get component from it. `ac = GameObject.Find("Sign_Wooden_Blank_01").GetComponent();` – Programmer May 08 '17 at 12:03
  • 1
    The-same thing still apply. Replace *Sign_Wooden_Blank_01* with the name of the camera when doing `GameObject.Find`. – Programmer May 08 '17 at 12:14
  • Thenks Programmer. – Daniel Halfoni May 08 '17 at 12:14
  • 1
    That's what the duplicated answer does. My first comment too.....Is it not working for you? You get the reference then you can call a function that script.... – Programmer May 08 '17 at 12:36

0 Answers0