- Home /
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. :(
Anyone knows why?
Thanks for the attention
regards
Matthew
I think you first have to cast the Component to a script:
randomcolor randomcolorscript = GameObject.Find("Capsule").GetComponent<randomcolor>();
randomcolorscript.ChangeColorObj(m_Capsualx,GameObject.Find("Capsule"), m_StartColorX, m_ColorTankX);
Coluld you, ExTheSea, be a little more precise, please? if is not a problem for you? I can't understandwhat u have written! :(
Answer by PAHeartBeat · May 29, 2013 at 03:38 AM
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.
Answer by Matthew_Crown · May 29, 2013 at 03:10 PM
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
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to access .int variable? 1 Answer
UnityEngine.Input.GetMouseButton(1)) issue 1 Answer
Comparing variable from another script, help? 1 Answer
GetComponent help needed CSharp 1 Answer