- Home /
Help for shots in a script
Hello everyone.
I'm not very good at compile scripts, so I ask you a hand. I wish in this script counted 10 shots and then off an object assigned to the script together with a sprite or texture always assigned to the script. Guitext must appear on the screen with numbers ranging from 10 to 0 every time I press the left mouse button. When the count reaches zero must disable the Inspector object associated with sprites. I insert the sript javascript test that I was writing. Thank you in advance.
var myTrigger : GameObject;
var myObject : GameObject;
var countAmmo : int = 10 ;
function Start()
{
}
function Update()
{
if(Input.GetButtonDown("Fire1"))
countAmmo = 0;
myObject.SetActive(false);
}
Answer by hbalint1 · May 03, 2015 at 12:13 PM
var myTrigger : GameObject;
var myObject : GameObject;
var countAmmo : int = 10 ;
function Start()
{
}
function Update()
{
if(Input.GetButtonDown("Fire1"))
{
// when you click, you should decrease the countAmmo
countAmmo--; //this is the same as: countAmmo = countAmmo - 1;
// if countAmmo is 0, then disabling object
if(countAmmo == 0) // '=' means assignment, '==' means "are the two side equals?"
myObject.SetActive(false);
}
}
Answer by machosalad · May 04, 2015 at 11:50 AM
using UnityEngine;
using System.Collections;
public class Drago10colpi : MonoBehaviour
{
public Sprite images;
const int maxCount = 10;
int count = maxCount;
void Update()
{
if (this.enabled && count > 0 && Input.GetButtonDown("Fire1"))
{
count--;
if (count < 1) // If the counter has reached 0...
{
// ...Then disable stuff
this.enabled = false;
images = null;
}
}
}
void OnGUI()
{
GUI.Label(new Rect(10, 10, 100, 20), count + "/" + maxCount);
}
}
That should do it :) If you are new to C# i recommend you look up some tutorials, this is very basic stuff.
Your answer
Follow this Question
Related Questions
Make button disappear, but show label. 1 Answer
How do I position a GUI Label over an object? 1 Answer
Playername in 3D World 2 Answers
Gui label variable as a string.. 1 Answer