Quantcast
Channel: Answers by "doomprodigy"
Viewing all articles
Browse latest Browse all 90

Answer by doomprodigy

$
0
0

Alright Firstly Have a script which contains all of your gloabal variables (So they can be called from all scripts, I like this way it keeps it nice and tidy.

So you might have

VariableScript.cs

and this script contains

using UnityEngine;
using System.Collections;

public class VariableScript : MonoBehaviour {
    public static int score = 0;
        // All your other variables can be put in here.

}

And then in your collider script having something like this we will name it ColiderScoreAdder

using UnityEngine;
using System.Collections;

public class ColiderScoreAdder : MonoBehaviour
{

// ball hits box, adds score then box destroys self (You could also instantiate an //explosion effect or the box made into parts with some physics
public GameObject spawnedprefab;
public float      explodeSecs = -1;

void Awake()
{
        if(explodeSecs > -1) Invoke ("DestroyNow", explodeSecs);
}

void OnCollisionEnter( Collision collision )
{
    // Rotate the object so that the y-axis faces along the normal of the surface
    ContactPoint contact = collision.contacts[0];
    Quaternion   rot     = Quaternion.FromToRotation(Vector3.up, contact.normal);
    Vector3      pos     = contact.point;
    Instantiate(spawnedprefab, pos, rot);
    // Destroy the projectile
    Destroy (gameObject);
}

void DestroyNow()
{
    Instantiate(explosionprefab, transform.position, transform.rotation);
    AddScore();
    Destroy (gameObject);
}

void AddScore ()
{
   VariableScript.score +=1;
}
}

Then with some fancy Gui Text Updating you could display your score in game (For whom may wonder I did edit the Colider explosion script from unify wiki for this)

For the Gui Text add a GuiText displaying 0 as the score to your game then in an update in one of your scripts put this somewhere (I named the gui text g_Score in this)

    print("Score is Now: "+VariableScript.score);
    GameObject.Find("g_Score").guiText.text = ""+VariableScript.score;

All done in C# Sorry if it is the incorrect language, It will work 100% though (If I mispelt anything and you get errors ask what the error is in this thread as a comment to me)


Viewing all articles
Browse latest Browse all 90

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>