3
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();  
    }  
}

Unity Debugger

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

4
  • Did you assign ClickToMove from inspector? Commented Dec 23, 2015 at 13:13
  • standard way of doing this in Unity is to 1. make your ClickToMove variable public, 2. select your object with WoodCuttingScript in hierarchy in Unity, drop the object with ClickToMoveScript in the ClickToMove slot in inspector. (keep WoodCuttingScript selected all the time, do not select ClickToMoveScript) Commented Dec 23, 2015 at 13:16
  • is ClickToMoveScript attached to another gameObject? Commented Dec 23, 2015 at 13:18
  • I not work with Unity3d some time ago but it looks just a object reference problem. Try to do ClickToMove = new ClickToMoveScript(); before ClickToMove.PlayWoodCuttingAnim(); or attach ClickToMoveScript to the gameObject and do gameObject.GetComponent<ClickToMoveScript>().PlayWoodCuttingAnim(); instead ClickToMove.PlayWoodCuttingAnim(); Commented Dec 23, 2015 at 13:24

5 Answers 5

9

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();  
}  
Sign up to request clarification or add additional context in comments.

4 Comments

2nd is delegate, if you want to play with delegates, then let me know
That is only valid if you have only one ClickToMoveScript.
Yups. if you have multiple then ClickToMoveScript[] ClickToMoveArray = FindObjectsOfType<ClickToMoveScript>(); then call foreach(ClickToMoveScript c in ClickToMoveArray ) c.PlayWoodCuttingAnim();
Thanks for the code@HamzaHasan this works like a charm :)
2

Try using .GetComponent<YourScriptName>.YourFunction().

Your function will (I believe) have to be public in order to use in another script :)

Comments

0

You should use GetComponent to get instance of another script.

    ClickToMove= gameObject.GetComponent("ClickToMoveScript") as ClickToMoveScript;
    ClickToMove.PlayWoodCuttingAnim();

Comments

0

Here is what i use:

FindObjectOfType<Sliding_Door>().Open_Door(1);

You can also use:

Sliding_Door door = FindObjectOfType<Sliding_Door>();
door.Open_Door(1);

Here is your version

FindObjectOfType<ClickToMoveScript>().PlayWoodCuttingAnim();

or

ClickToMoveScript init = FindObjectOfType<ClickToMoveScript>();
init.PlayWoodCuttingAnim();

Comments

-3

You need to create a new istance of clicktomove like ClickToMoveScript ClickToMove = new ClickToMoveScript();

1 Comment

This is not true, you cannot instatiate MonoBehaviour.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.