- Home /
I found the i put a null in another script and i forgot about it
gameObject are not referenced
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;
}
@DiegoSLTS i have updated the question with getData
never$$anonymous$$d, problem on my side. Closed
Answer by cgklutts · Dec 25, 2019 at 02:44 PM
From what I gather you need to change...
dataCol.hittedBall = gameObject;
To this...
dataCol.hittedBall = colInfo.gameObject;
Answer by secchierojacopo · Dec 25, 2019 at 03:10 PM
thanks for the help, but this is not what i want @cgklutts.
I want to pass the current gameObject owner to the dataCol
structure.dataCol.hittedBall = gameObject
it's fine, in fact dataCol.hittedBall
contain correctly the object if i try to read from onCollisionEnter2D()
But if i try to read from Update()
i get null and Debug.Log("GAMEOBJECT HERE")
never happen.
Follow this Question
Related Questions
How to associate underlying data structure to prefab at runtime? 1 Answer
How do I make prefab follow a gameobject?(C#) 1 Answer
Trouble with destroying an instantiated prefab 2 Answers
Reference Player When Instantiating A Prefab 0 Answers
How to SetActive(false/true) on an instantiated object, being a clone? 1 Answer