Basically what I’m trying to do is have it so whenever I place an object into a trigger area I want it to add to the players score, and also show on the GUI? How should I do this?
Code so far for the Trigger Area:
using UnityEngine;
using System.Collections;
public class TriggerZone : MonoBehaviour
{
void OnTriggerEnter (Collider other)
{
Debug.Log ("Object Entered Trigger.");
}
void OnTriggerStay (Collider other)
{
Debug.Log ("Object is within trigger.");
}
void OnTriggerExit (Collider other)
{
Debug.Log ("Object has exited trigger.");
}
}
well to show things in the ui it is more complicated. Anyway here are many tutorials.
What you have to do is hit add game object,select ui, select text and then place your text. to place it well i recommend you looking at the game view.
once you have done that do this:
Make a script. Attach it to a game object.
-and write this.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class NewBehaviourScript : MonoBehaviour {
public Text MyText;
private int score;
// Use this for initialization
void Start () {
MyText.text = "";
}
// Update is called once per frame
void Update () {
MyText.text = "" + score;
}
void OnTriggerEnter(Collider coll) {
score = score + 1;
}
}
note: this will only do that when you collide with any trigger a text comes up
Well I got it working the way I wanted it to, but now I am trying to have it delete the object I put in, and exclude the Player so you can’t just walk inside of the trigger zone to score.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TriggerZone2 : MonoBehaviour {
public Text MyText;
private int score;
public bool CompareTag(string Player);
// Use this for initialization
void Start () {
MyText.text = "";
}
// Update is called once per frame
void Update () {
MyText.text = "$" + score;
}
void OnTriggerEnter(Collider coll) {
score = score + 1;
if (other.CompareTag ("Player")) {
Destroy (other.gameObject);
}
}
}
The console keeps saying “Assets/Scripts/TriggerZone2.cs(11,21): error CS0501: `TriggerZone2.CompareTag(string)’ must have a body because it is not marked abstract, extern, or partial.” I’ve looked up quite a few threads, but I’m still not sure what it wants me to do with it.
Gametyme: I did check that tutorial out earlier. It is actually how I figured out how to use some of this.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TriggerZone : MonoBehaviour {
public Text MyText;
private int score;
// Use this for initialization
void Start () {
MyText.text = "";
}
// Update is called once per frame
void Update () {
MyText.text = "$" + score;
}
void OnTriggerEnter(Collider coll) {
if (coll.CompareTag ("Pickable")){
score = score + 1;
}
if (coll.CompareTag ("Pickable")) {
Destroy (coll.gameObject);
}
}
}
I was wondering how could I make a second trigger zone (that gives a different value), and adds that to the score (on top of the current score)?
Also how should I handle this? “MissingReferenceException: The object of type ‘GameObject’ 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.”
that MissingReferenceException happens because you destroyed a game object and you tried to access it later so, in the line it saysDestroy (coll.gameObject);
put
coll.gameObject.SetActive(false);
Also to make a second trigger zone I would personally use prefabs. If you want to know how this works ask me, i know.
well, I think this might be a useful answer. Create a prefab in the project view, attach your pickable to the prefab. then create an empty game object. Attach your pickable to that empty game object. After doing this duplicate your pickable and it should work.
I cannot guarantee I’m right, I’m a noob myself. If you have any questions don’t be afraid to ask!
As a suggestion, you don’t need to update the score every frame, you currently set it on update. Instead just update the field when the score is changed. Or if you are updating the value from another script, use getters/setters. It’s a good habit to get into.