GetComponent is missing custom function Call. Why?

OK strange issue this time.
I am practicing with getcomponent and findwithtag (or find) commands, in order to apply a custom function created in the script of a first object (in this case a cube) to the second (a capsule object). Unfortunately this is not working and I don’t know why, so I can’t call my custom function from a script to another.

In my scene I have a capsule and a cube placed in its public gameobject slot and tagged “cubo”(the cube) and “capsula” (the capsule).

The custom function I created is called ChangeColorObj, it add a color to a GameObject selecting it randomly from a color array.
Here u are the script of the cube object:

using UnityEngine;
using System.Collections;

public class randomcolor : MonoBehaviour {
	public GameObject m_Cube = null;
	public int m_RandomEngine = 0;
	public Color[] m_ColorTank;
	public Color m_ColorStart;
		
	
	
	void Start () {
	
		ChangeColorObj (m_RandomEngine, m_Cube, m_ColorStart, m_ColorTank);

		
	}
	
	
	
		public void ChangeColorObj (int arandom, GameObject cb, Color prefab, Color [] list ) 
	{
		arandom = Random.Range (0, list.Length);
		prefab = list[arandom];
		cb.renderer.material.color = prefab;
	}
	
	
	void Update () {
	
	}
}

and now the script of the capsule object:

using UnityEngine;
using System.Collections;

public class capsularandom : MonoBehaviour {
	
	public GameObject m_Capsule = null;
	public int m_Capsualx = 0;
	public Color[] m_ColorTankX;
	public Color m_StartColorX;
	
	
	void Start () {
	
		
	//m_Capsula.GetComponent<randomcolor>().CambiaColorePerOBJ(m_Capsualx, m_Capsula, m_Inizialx, m_SerbatoioColorx);
	GameObject.Find("Capsule").GetComponent<randomcolor>().ChangeColorObj(m_Capsualx,GameObject.Find("Capsule"), m_StartColorX, m_ColorTankX);
	//GameObject.FindWithTag("capsula").SendMessage("CambiaColorePerOBJ",(m_Capsualx, GameObject.FindWithTag("capsula"), m_Inizialx, m_SerbatoioColorx));
	
	}
	

	void Update () {
	
	}
}

notice that there are 2 commented strings: they are other attempts that doesn’t works too.
The scripts are without errors, because if I press f8, all seems to be ok.
But only the cube change its color, instead the capsule still remains gray. :frowning:

Anyone knows why?

Thanks for the attention

regards

Matthew

Try this,

public class capsularandom : MonoBehaviour {
 
    public GameObject m_Capsule = null;
    public int m_Capsualx = 0;
    public Color[] m_ColorTankX;
    public Color m_StartColorX;
 
 
    void Start () {
        m_Capsule = GameObject.Find("Capsule");
        m_Capsule.GetComponent<randomcolor>().ChangeColorObj(m_Capsualx,m_Capsule, m_StartColorX, m_ColorTankX);
    }
 
    void Update () {
    }
}

if you need more accurate solution please randomcolor script also.

Ok after days and days I discover the problem.

In the second script I called the object target (the capsule) instead to call the object that keep the methond, namely: the cube.

here u are:

GameObject.FindWithTag(“cubo”).GetComponent().ChangeColorObj(m_Capsualx,GameObject.FindWithTag(“capsula”), m_StartColorX, m_ColorTankX);

thanks a lot for the attention, anyway!

matthew