using UnityEngine; using System.Collections;
public class ClickToMoveScript : MonoBehaviour
{
public string Chopping = "Chopping";
public void PlayWoodCuttingAnim()
{
//Play Woodcutting Animation
gameObject.GetComponent<Animation>().CrossFade (Chopping);
}
}
Heres my other Script
using UnityEngine;
using System.Collections;
public class WoodCuttingScript: MonoBehaviour
{
ClickToMoveScript ClickToMove;
void Start()
{
ClickToMove.PlayWoodCuttingAnim();
}
}

I have already added the animation inside the animation component.
The other thing i found out is that if i call the PlayWoodCuttingAnim() function inside ClickToMove script it works fine but in the other script it dosent work.
The error console > NullReferenceException: Object reference not set to an instance of an object.
Any help would be greatly appreciated
Well, there are few common methods to do so.
Like calling method from another script you will need to get attached (to gameObject) script instance instead of simple script instance.
You can do it by,
void Start()
{
ClickToMove = FindObjectOfType<ClickToMoveScript>();
ClickToMove.PlayWoodCuttingAnim();
}
Try using .GetComponent<YourScriptName>.YourFunction().
Your function will (I believe) have to be public in order to use in another script :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With