Unable to access parent gameobjects scripts variable

I have a game object Ship with a script Ship.cs this game object then has a gameobject Child FillerShipRender which has 24 children named MountPointCannon.

Ship.cs

using UnityEngine;
using System.Collections;

public class Ship : MonoBehaviour {

	public int cannonballs;
	public GameObject CannonType;

	// Use this for initialization
	void Start () {
		populateMountPoints ();
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	public int getCannonballs()
	{
		return cannonballs;
	}

	void populateMountPoints()
	{
		GameObject clone = new GameObject("myClone");
		foreach (Transform t in GetComponentsInChildren<Transform>()) 
		{
			if (t.name == "MountPointCannon")
				clone = Instantiate (CannonType, t.position, t.rotation) as GameObject;
			clone.transform.parent = transform;	
		}
}
}

When run this instantiates 24 cannons using the MountPointCannons for the locations. Each cannon has a ShootCannon.cs script. I would like to have the ShootCannon script access the Ship.cs to determine how many cannonballs are available and take one to shoot. When I use the commented out line it does not work.

using UnityEngine;
using System.Collections;

public class ShootCannon : MonoBehaviour
{
	public Rigidbody projectile;
	public Transform barrelEnd;
	public int force;
	public Quaternion rotation;



	void Start()
	{

	}

	void Update ()
	{
		if(Input.GetButtonDown("Fire1"))
		{
			Rigidbody ball;
			ball = Instantiate(projectile, barrelEnd.position, rotation) as Rigidbody;
			ball.AddForce(barrelEnd.right * force);

			//Debug.Log (transform.parent.GetComponent(Ship).Cannonballs);

		}
	}
}

I get the following errors in unity when I uncomment this line

Also in MonoDevelop “Cannonballs” within the commented area is displayed in red…

I have no idea how to reference another script looked online and this seemed the right way to do so in this situation but I guess I am wrong. Any help would be appreciated.

I guess you first have to make an object of the ship like so:

Ship myship;
I am not sure exactly how your scripts are related but you have to make sure if you reference another object with a script that you use not only GetComponent but also “Find”, so the compiler can actually find the objects that you are looking for. Also you spelled capitalized cannonballs in your second script which is different from your first script.
If you made the object, write myship.cannonballs and you should be fine.

You are getting an error because GetComponent expects a type System.Type. Yes that’s the type it wants, System.Type.

// GetComponent(System.Type) expects a System.Type.
Debug.Log (transform.parent.GetComponent(Ship).Cannonballs); // Error!
// We can get the System.Type by using the typeof keyword.
// Note:  Casting "as Ship" because GetComponent returns type Component, not Ship.
Debug.Log ((transform.parent.GetComponent(typeof(Ship)) as Ship).Cannonballs);
// I prefer using the generic function.
Debug.Log (transform.parent.GetComponent<Ship>().Cannonballs);

EDIT: Also, based on your description, you might need to get transform.parent.parent. (parent.parent.parent…)

Thank you Garth you were right on the money.

This gave me what I needed so I can move forward with programming many thanks.