How can i restart a script on collision ?
How can I restart a script when a player collides with an object?
For some reason, I can't reply to the comment below. But what I would do is this. On the oncollisionenter call the function. In the function, you instantiate all the gameobject needed. something like this maybe
void OnCollisionEnter()
{
spawnthings();
}
void spawnthings()
{
Instantiate(gameobject, transform.position, Quatorian.identy);
}
I might write something wrong but you get the idea.
if you don't understand the Instantiate thing
look at this: https://docs.unity3d.com/ScriptReference/Object.Instantiate.html
Answer by aardappel156 · Jun 18, 2018 at 05:14 PM
What do you exactly mean with a restart?
can you elaborate what you are trying to make.
And can you post your script here?
So I'm doing a runner with random generation of objects, and what I wanted to do is when the player collides with a specific object, the random generation script is restarted
This is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Generator : $$anonymous$$onoBehaviour {
public float startDistance = 10;
public float yDistance = 100;
public float $$anonymous$$Spread = 5;
public float maxSpread = 10;
public Transform playerTransform;
public Transform obstaclePrefab;
float ySpread;
float lastYPos;
void Start()
{
ySpread = Random.Range($$anonymous$$Spread, maxSpread);
lastYPos = playerTransform.position.y + (startDistance - ySpread - yDistance);
}
void Update()
{
if (playerTransform.position.y - lastYPos >= ySpread)
{
float lanePos = Random.Range(0, 3);
lanePos = (lanePos - 1) * 1.5f;
lastYPos += ySpread;
ySpread = Random.Range($$anonymous$$Spread, maxSpread);
}
}
}
$$anonymous$$aybe you can write a function where you generate that and call that function whenever the player collides with something.
for example
void randomgen() { //put the code how you generate your gameobjects }
void OnCollisionEnter(Collision collision) { if (collision.gameobjects.tag == "give that object this tag" { randomgen() } }
I haven't tried it but this might work.
Your answer
Follow this Question
Related Questions
How to call an action from a script that is on the anonymous object you collide with? 1 Answer
How to have a game object register its OnTriggerEnter function? 1 Answer
OnCollisionEnter - warning in 3D Test Game 0 Answers
Physics.CheckSphere get collision GameObject 0 Answers
Why is my trigger collider acting like a normal collider? 1 Answer