Prefab instatiation not going well

I'm trying to Instantiate a prefab when the mouse goes over collider of some GameObject so I'm using OnMouseEnter to instantiate and will use OnMouseExit to destroy it after (when this part of code works fine), the problem is that no error appears in the console but the prefab are not being instantiated, does not appear in the inspector or in the scene, even so, when I print it's name in the console (last line), it appears right. Is my syntax correct?

void OnMouseEnter ()
{
string nomeSeta = "SETA" + direcao.getNome ();
float[] pontosXeZ = Util_CalculosGeometricos.ObterPontosCentraisXeZDeUmRetanguloPorNumeracaoSemRotacao (terrenoPai, direcao.getCoeficienteHorario ());
seta.transform.localPosition = new Vector3 (pontosXeZ [0], 0.0f, pontosXeZ [1]);
seta.name = nomeSeta;
print ("seta.name: " + seta.name);
}


Replace

GameObject seta = Resources.Load<GameObject> (nomeSeta);


with

GameObject seta = Instantiate(Resources.Load<GameObject>(nomeSeta));


This will load the GameObject from your resources directory and then instantiate it into the scene. Otherwise it's just data local to the scope of your method (which is why your print method works).

I'm not in front of Unity so I can't verify, but I think you need to do Object.Instantiate rather than Resources.Load

• Actually you may need both; Resources.Load to load the asset from your Resources folder, and then Instantiate to copy it into the scene. – jhocking Jun 30 '17 at 18:02
• Correct, what I do is assign the prefabs to my scripts I think. So if I have a GameObject that has a Script on it, and that Script is supposed to instantiate a prefab, then I typically have a public property on that Script for the type of Prefab to instantiate, and then use Object.Instantiate on that. I think I prefer that over Resources.Load and having the script know exactly what prefab to load. – Jeff Sheldon Jul 5 '17 at 15:11
• I would tend to prefer that too but there is one significant downside: the prefab will be in memory as long as the script exists. Depending on the nature of the prefab in question, that may not matter (maybe that prefab needs to be loaded all the time anyway) but, well, maybe it does matter. – jhocking Jul 5 '17 at 15:22
• Good point! Good to know. I hadn't thought about it from that perspective. I suppose if you have a collection of Prefabs that you may or may not even load during that launch of the game, you could heavily benefit from selectively loading what you need to instantiate. I haven't come across that yet, but that makes sense. – Jeff Sheldon Jul 5 '17 at 15:26
• I come across that often in the games I work on. I develop CCGs and RPGs, so anything with a massive inventory like those you are not going to want to load all possible cards/weapons/etc. ahead of time. – jhocking Jul 5 '17 at 15:30