- Home /
Detect collision of a GO within an instantiated prefab, against my player.
Sorry for the confusing title, I'm still not sure the best way to describe things.
I have done a basic endless runner, I changed it a few weeks back so that the track scrolls towards origin, rather than the player infinitely scroll.
I have a Tilemanager that is responsible for creating the tiles - I recently added some collider and rigidbody components to vehicles on my tile, as below:
My issue is that I need to stop the tile manager scrolling the instantiated tiles and stop the score (accessible on TileManager) when one of the colliders, within my Instantiated prefab, hits my player.
At the minute, if I add:
void OnCollisionEnter(Collision other)
{
GameOver();
}
private void GameOver()
{
Debug.Log("Dead");
}
To the PlayerMovement script (on Player), the collision works and the note is triggered - However, when I add it to the TileManager script, it doesn't ever trigger.
I have tried adding the rigidbody to my player and removing it from the vehicles within Instantiated tiles but that didn't work either.
Is there any way I can do this? Sorry if my explanation isn't great.
Thanks a lot for any help.
Answer by Larry-Dietz · Dec 04, 2019 at 03:16 PM
How about giving the player script a reference to the tile manager, and in the OnCollisionEnter on the player, call a method in TileManager to handle whatever changes you want to happen, i.e. stop scrolling, etc...
-Larry
Hey @Larry-Dietz, thanks for the response!
How do I reference one script from another without it being a component?
Can I just do:
private GameObject tile$$anonymous$$anager;
void Start() {
tile$$anonymous$$anager = GameObject.FindObjectWithTag("Tile$$anonymous$$anager");
}
Thanks :) if I can get hold of it, I can use it no problem, I just haven't gotten hold of it. I'll try the above when I'm in but if it's not going to work or there's a better way, let me know.
Thanks again.
Your best best would be something like this, assu$$anonymous$$g only one Tile$$anonymous$$anager exists in the scene...
Private Tile$$anonymous$$anager tile$$anonymous$$anager;
void Start()
{
tile$$anonymous$$anager = FindObjectOfType<Tile$$anonymous$$anager>();
}
The you can just directly call any public methods in tile$$anonymous$$anager, i.e.
tile$$anonymous$$anager.PauseScrolling();
Hope this helps, -Larry
Thank you, it did help! I feel stupid for how simple and obvious that was. Thanks for pointing me in the right direction
Your answer
Follow this Question
Related Questions
Box Colider not working properly 1 Answer
Sound volume on different colisions 1 Answer
Proper instantiate projectile with variables (C#) 1 Answer
Coliders Don't work 1 Answer
My Position suddenly changes 0 Answers