How can I access a component in a parent object?

Hello.

I’m raycasting on an object whom has a parent and I want to access a component on the parent, how do I do this?

this code:

void Update () {
		Cylinder otherobject;
RaycastHit hit;
		Vector3 angle0 = transform.TransformDirection(Vector3.up);
		if (Physics.Raycast(transform.position, angle0, out hit, 1)){
			otherobject = hit.collider.gameObject.transform.parent.GetComponent	<script>().something();
}
}

How can I access a function on the script? the error I get:
Cannot implicitly convert type void' to Cylinder’

How can I fix this?

Thanks!

That error shows that the method “something()” is not returning anything as it is declarated like this:

public void something() {
    //code
}

Just be sure that the method you are using from the parent returns a Cylinder.

The way you are accessing the component in the parent is right although I would just get it as follows:

if (Physics.Raycast(transform.position, angle0, out hit, 1))
{
    otherobject = hit.transform.parent.GetComponent<script>().something();
}