I have this script in my prefab, after i instantiate it all works good, but can’t reference the gameObject itself outside OnCollisionEnter2D().
public class getCollisionBall : MonoBehaviour
{
getData dataCol = null;
void OnCollisionEnter2D(Collision2D colInfo)
{
if (colInfo.gameObject.name.Contains("Player"))
{
dataCol = colInfo.gameObject.GetComponent<getData>();
dataCol.hittedBall = gameObject;
Debug.Log(gameObject); //THIS WORKS! is the gameObject
dataCol.vanish = true;
Debug.Log(gameObject); //THIS WORKS! is true
}
}
void Update()
{
if (dataCol != null
&& dataCol.hittedBall != null) {
Debug.Log("GAMEOBJECT HERE"); //THIS NOT WORKS! is null, but i excpet the gameObject here
}
if (dataCol != null
&& dataCol.vanish) {
Debug.Log("VANISH HERE"); //THIS WORKS! is true
}
}
}
public class getData : MonoBehaviour
{
public GameObject hittedBall = null;
public bool vanish = false;
}