- Home /
Cannot implicitly convert type `UnityEngine.GameObject' to `UnityEngine.Transform'
Hello,
I checked other questions and I don't understand why I receive this common error. Here are the important parts of the code :
public static GameObject particle;
public static GameObject clickedGmObj;
public static GameObject damagePrefab;
public static GameObject healPrefab;
......
void Update () {
GameObject clickedGmObj = null;
if(Input.GetMouseButtonDown(0))
{
clickedGmObj = GetClickedGameObject();
......
GameObject GetClickedGameObject()
{
Ray ray = UICamera.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray,out hit))
{
return hit.transform.gameObject;
}
else
{
return null;
}
}
.......
public void TeamCast()
{
var bounds = hit.collider.bounds;
var v3 = bounds.center;
v3.y -= bounds.extents.y;
damagePrefab = Instantiate(particle, v3, transform.rotation) as GameObject;
damagePrefab.transform.parent = clickedGmObj;
And here there are all the lines / functions dealing with spawning gameobjects, saving them or altering them. Why it gives me the error for the line : damagePrefab.transform.parent = clickedGmObj;
What I want to achieve is attach the spawned particles to the detected collider in the game which is a unit, so the particle effect will follow my unit around.
Thanks
Errors are gone but in game, the console informs me that there is no object reference set to an instance of an object.
Do you guys know if return hit.transform.gameObject; is really storing the clicked collider ?
Check if you call the TeamCast()
function only if you hit something. Perhaps the clickedGmObj
is null ( GetClickedGameObject()
returned null) and cannot be set as parent.
It works. I freaked at first because I was having an if condition, which in that case wasn't enabling the rest of the function :)
Thanks for the help guys !
Answer by ExTheSea · May 31, 2013 at 07:12 PM
Try:
damagePrefab.transform.parent = clickedGmObj.transform;
transform.parent expects a variable of type Transform while clickedGmObj is of type GameObject.