How to call a function of grandparent

I tried calling the GetNewWaypoint function using this code:

using UnityEngine;
using System.Collections;
namespace UnityStandardAssets.Utility {
	public class WaypointChanger : MonoBehaviour {

		private WaypointCircuit circuit;

		void OnTriggerEnter(Collider other) {
			circuit = gameObject.transform.root.gameObject.GetNewWaypoint ();

			other.gameObject.transform.root.SendMessage ("ChangeWaypoint", circuit);
			other.gameObject.transform.root.SendMessage ("Reset");

		}
	}
}

But it gives me this error message:
Assets/Scripts/WaypointChanger.cs(9,72): error CS1061: Type UnityEngine.GameObject' does not contain a definition for GetNewWaypoint’ and no extension method GetNewWaypoint' of type UnityEngine.GameObject’ could be found (are you missing a using directive or an assembly reference?)

Also, here’s the function I wanted to call:

function GetNewWaypoint () : UnityStandardAssets.Utility.WaypointCircuit {
	return instances[1].GetComponent(TerrainGeneration).routes[0];
}

Just to clarify, the function you are trying to call (from C# I presume) is JavaScript.

First of all, this is how you access a “grandparent” in C# (Call C# function from C#)

transform.parent.parent.gameObject.myAwesomeFunction();

Alternatively, you can cache the GO:

GameObject grandpa = transform.parent.parent.gameObject;
grandpa.myFirstAwesomeFunction();
grandpa.mySecondAwesomeFunction();

The solution to calling JavaScript functions from C# could be something like this(Call JS function from C#):

// Getting the grandparent GameObject
GameObject grandpa = transform.parent.parent.gameObject;

// Getting the JavaScript script on that GameObject (in this case, the script name is MyJS_ScriptName
var jsScript = (grandpa.GetComponent(typeof(MyJS_ScriptName)) as MyJS_ScriptName);

// Calling the JavaScript function
jsScript.GetNewWaypoint();

You have to pay attention to compilation order which MIGHT be a problem sometimes… Quoting from this question’s second answer:

You’ll probably have to add this
Javascript into Standard Asset’s
Folder because of the script compiling
order. That will prevent you from
sending messages on a two side bridge.
You’ll be able to call methods from
one side only. I had this problem
myself and decided to move all my
codebase to csharp, to avoid this
problem.

Bare in mind I might be wrong about the last part. Should problems arise, please do more research regarding that matter.

In order to call a script from another GameObject and use a function from it you have to get the Script component from the GameObject first, like this:

public class ObjWithScript : MonoBehaviour {

	public void CallThisFunction()
    {
        Debug.Log("Some Function");
    }
}


public class ObjCallingScript : MonoBehaviour
{
    public GameObject obj; //Assign your object with script here

    void Start()
    {
        ObjWithScript yourScript = (ObjWithScript)obj.GetComponent(typeof(ObjWithScript));
        yourScript.CallThisFunction();
    }
}