- Home /
Method doesn't work when called in other script
Hello. I got a problem with my method TriggerBlocks(). This method works well when its called on the script in which is made, so when I call it on void Start() in Block.cs it works well and every "Block" game object is becoming a trigger but this isn't the place where I want to call this method. But when I call it in script Paddle.cs I got error "MissingReferenceException: The object of type 'Block' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object."
So this should work that when gameObject with tag: "Through Square" collides with "Paddle" then every destructable Block on the scene is becoming trigger.
Method TriggerBlocks() in Block.cs:
public void TriggerBlocks()
{
GetComponent<Collider2D>().isTrigger = true;
}
Calling TriggerBlocks() method in OnTriggerEnter2D in Paddle.cs (Also "Block block" is initialized at the beginning of the script)
if (collision.gameObject.tag == "Through Square")
{
block.TriggerBlocks();
Destroy(GameObject.FindWithTag("Through Square"));
}
Also sorry for not sharing entire scripts but this is important for me because I'm doing this game for my university degree. But I can share more if this will be needed.
Your answer
Follow this Question
Related Questions
How to select Object's voids through Inspector tab? 2 Answers
collider vs collider2d 1 Answer
OnTriggerEnter2D Doesn't work? ;_; 4 Answers
2D Animation does not start 1 Answer