- Home /
Click To Revert to Original Texture else Destroy script help
Need Help with scripting problem. I am trying to make a City Building game where the building will randomly destroy if not clicked. The script does change the texture to "txture" and destroy it. The problem now is that the script still doesn't change/revert to "original." i Think the problem lies with the Raycast problem. Any solution for this will be helpful. Thanks
var Txture : Texture;
var Original : Texture;
function Start ()
{
InvokeRepeating ("Riot", 1, 1);
}
function Riot() {
var RiotRandom : int = Random.Range(1,100);
if (RiotRandom > 1) {
renderer.material.mainTexture = Txture;
}
InvokeRepeating ("Destroyz", 5, 1);
}
function Destroyz() {
if(Input.GetMouseButtonDown(0)){
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if(Physics.Raycast(ray,hit, 150) &&(hit.transform.tag == "Bd1"))
{
renderer.material.mainTexture = Original;
}
}
else
{
Destroy(gameObject);
}
}
Note: Bd1 is the building tag & I Put this script at the building, not camera
Answer by robertbu · Jun 08, 2013 at 03:12 PM
There are a couple of strange thing here. First on Line 14, you have a InvokeRepeating(). This is going to cause a new InvokeRepeating() every time Riot() is called (without canceling the old one). You probably want just Invoke(). Second, GetMouseButtonDown() is only true for a single frame that the button is held down, so the chance that it will be true for the same frame that Destroyz() is called is vary small. Here is a bit of a rewrite to your code that gets closer to what I think you want:
var Txture : Texture;
var Original : Texture;
private var doDestroy = false;
function Start () {
InvokeRepeating ("Riot", 1, 1);
}
function Riot() {
var RiotRandom : int = Random.Range(1,100);
if (RiotRandom > 1 && !doDestroy) {
renderer.material.mainTexture = Txture;
doDestroy = true;
Invoke("Destroyz", 5);
}
}
function Destroyz() {
Debug.Log(doDestroy.ToString());
if (doDestroy)
Destroy(gameObject);
}
function OnMouseDown() {
renderer.material.mainTexture = Original;
doDestroy = false;
}
Thanks!! It works. But there is small problem. I want the script to repeat back to InvokeRepeating Riot(). so it will keep repeating until the game ends or fail to click and destroyed.
You'll have to explain more. Riot() still continues to repeat. But you will not know how soon or how far you click the next one will come. If you want it to wait for whatever time you are currently using for InvokeRepeating(), you will need to get rid of InvokeRepeating() and use your owner timestamp. Here is an exmaple:
var Txture : Texture;
var Original : Texture;
var nextRiot = 1.0;
private var timeStamp;
private var doDestroy = false;
function Start () {
timeStamp = Time.time + nextRiot;
}
function Update() {
if (timeStamp < Time.time) {
Riot();
TimeStamp = Time.time + nextRiot;
}
}
function Riot() {
var RiotRandom : int = Random.Range(1,100);
if (RiotRandom > 1 && !doDestroy) {
renderer.material.mainTexture = Txture;
doDestroy = true;
Invoke("Destroyz", 5);
}
}
function Destroyz() {
if (doDestroy)
Destroy(gameObject);
}
function On$$anonymous$$ouseDown() {
renderer.material.mainTexture = Original;
doDestroy = false;
timeStamp = Time.time + nextRiot;
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
GuiTexture Width Change 1 Answer
Help needed with a pickup script 2 Answers
Space Invaders movement? 1 Answer
Script to disable MouseLook? 2 Answers