- Home /
what to change in the script that it calls start from a external button other then when collider is detected
I have this script that counts the balloons destroyed along with a timer it starts when a collider in my case a paper plane collides with it I want to change it starts when I press a button from my other UI
using UnityEngine;
using System.Collections;
namespace DaydreamElements.SwipeMenu {
[RequireComponent(typeof(AudioSource))]
public class Sign : MonoBehaviour {
private TextMesh textMesh;
private float timerEnd;
private int prevTime;
private AudioSource audioSource;
private static bool started = false;
private static int score = 0;
public GameObject signText;
public static void IncScore() {
if (started) {
++score;
}
}
void Start() {
audioSource = GetComponent<AudioSource>();
textMesh = signText.GetComponent<TextMesh>();
Debug.Assert(textMesh != null);
}
void Update() {
if (started) {
int newTime = (int)(timerEnd - Time.realtimeSinceStartup) + 1;
if (newTime <= 0) {
started = false;
textMesh.text = "Game Over!\nShoot again to restart\nYour score: " + score + "\n";
} else if (newTime != prevTime) {
prevTime = newTime;
textMesh.text = "00:" + newTime.ToString("00") + "\nYour score: " + score + "\n";
if (newTime == 5) {
audioSource.Play();
}
}
}
}
void OnCollisionEnter(Collision collision) {
RigidPaperAirplane rigidPencil = collision.gameObject.GetComponent<RigidPaperAirplane>();
if (rigidPencil == null) {
return;
}
if (!started) {
started = true;
timerEnd = Time.realtimeSinceStartup + 60.0f;
prevTime = 0;
score = 0;
}
}
}
}
Answer by tormentoarmagedoom · May 15, 2019 at 10:42 AM
Hello.
Then just move everything inside OnCollisionEnter() into a new void method called for example StartTime()
And in Update just add this:
if (Input.GetKeyDown(KeyCode.Space))
{
StartTime();
}
Add any other condition if you need.
Bye!
it says collision does not exist in current context
void Startcount()
{
//it says that collision dosent exist
RigidPaperAirplane rigidPencil = **collision**.GetComponent<RigidPaperAirplane>();
if (rigidPencil == null)
{
return;
}
if (!started)
{
started = true;
timerEnd = Time.realtimeSinceStartup + 60.0f;
prevTime = 0;
score = 0;
}
Oh come on... move everything but adapt to new situation.... you are not iside ontrigger methood, ther is no colliders...
I think You should watch some tutorials and learn more before continue..
Follow this Question
Related Questions
how to allow the key to only open 1 door rather than all of them? 0 Answers
(PLEASE HELP) hi guys! i have a problem with my Score text 1 Answer
Wait for animation to finish 2 Answers
How do I make my projectile deal damage to a target it hit. 1 Answer
How do I make do I make a variable go back to its original value for a spell system 1 Answer