Question by
AxetheRoad · May 27, 2020 at 10:53 PM ·
colliderontriggerenterif statementenable and disable script
How to enable a portal when reaching a certain amount of score?
Hi! I am a new user to Unity! I am making a 3D game which consist of the player collecting 20 keys in order to make a portal to appear and end the game. When I run the game, the portal is on and when I finish collecting the 20 keys, the portal goes on and off.
All comments are appreciated!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public Text countText;
public Text winText;
public int count;
public GameObject PortalEnd;
private bool isOn;
void Start ()
SetCountText();
winText.text = "";
count = 0;
isOn = false;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Keys"))
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText();
}
}
void SetCountText()
{
countText.text = "Keys obtained: " + count.ToString() + " of 20";
if (count >= 20)
{
winText.text = "You Win!";
}
}
void Update()
{
if (count >= 20)
{
if (isOn == true)
{
PortalEnd.SetActive(false);
isOn = false;
}
else if (isOn == false)
{
PortalEnd.SetActive(true);
isOn = true;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Calling "OnTriggerEnter" when a parent object has a rigidbody 0 Answers
OnTriggerEnter playing without tagged object triggering 0 Answers
How to check for collisions only with certain colliders on a gameobject? 0 Answers
Unity 3D(C#) - How to connect more than two box colliders used as triggers? 1 Answer
OnTrigerEnter not working 2 Answers