- Home /
Can't activate my timer for shooting script?
I have two scripts, I'm attempting to make my first script use a timer (my second script) and I'm not very good at referencing so I decided to try it out and understand it better. so I created the first script and as far as I can tell, in my second script the timer should be counting down since the console is returning the Debug.Log ("true"); however the tickbox for public bool IsTiming; is set to true once I have started? I'm pretty new to c# so probably just a noob problem :/
using System.Collections;
public class WeaponFireattempt2 : MonoBehaviour
{
public float bullets = 7f;
public GameObject deagle;
public bool animationreloadplayed = false;
public bool fired = false;
public float Distance;
public float MaxDistance = 100;
public float Damage = 25;
public bool Reloading = false;
public bool timedown;
public float time = .75f;
public bool firedshot = false;
public bool haswaited = false;
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Input.GetButtonDown ("Fire1") && animationreloadplayed == false)
{
firedshot = true;
fired = true;
}
if (bullets <= 0f && bullets < 7)
{
animationreloadplayed = true;
}
if (fired == true)
{
RaycastHit hit;
{
if(Physics.Raycast (transform.position, transform.TransformDirection(Vector3.down), out hit))
{
Distance = hit.distance;
if (Distance < MaxDistance)
hit.transform.SendMessage ("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
Debug.DrawLine(transform.position, hit.point, Color.red);
fired = false;
}
}
}
if (Input.GetKeyDown (KeyCode.R))
{
animationreloadplayed = true;
}
if (animationreloadplayed == true)
{
deagle.animation.Play ("Reload");
bullets = 7f;
if (bullets == 7 && animationreloadplayed == true)
{
animationreloadplayed = false;
}
}
if (firedshot == true)
{
transform.SendMessage ("beginTimer");
if (haswaited == true)
{
bullets = bullets -1f;
deagle.animation.Play ("Fire");
firedshot = false;
audio.Play ();
haswaited = false;
}
}
}
}
Second script
using UnityEngine;
using System.Collections;
public class Force : MonoBehaviour
{
public float timer = 0f;
public bool IsTiming = false;
public float NumberOfSecondsToWait = 1f;
public GameObject deagle;
void beginTimer()
{
timer = 0f;
IsTiming = true;
if(IsTiming == true)
{
timer = timer * Time.deltaTime;
{
Debug.Log ("true");
}
}
if (timer < NumberOfSecondsToWait)
{
deagle.GetComponent<WeaponFireattempt2>().haswaited = true;
}
}
void EndTimer()
{
IsTiming = false;
timer = 0f;
}
}
Answer by Nightdr · Mar 31, 2014 at 10:36 PM
Please watch this [Bucky's Scope Tutorial][1]. Sorry if my post is long, but it will teach you a great thing about code that you can use later on. When you are trying to access or modify a variable in another class then you need to put the name of that class first so something like this:
using UnityEngine;
using System.Collections;
public class Testing2 : MonoBehaviour {
public static int NewIntYAY = 1;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
Another Class!
using UnityEngine;
using System.Collections;
public class Testing1 : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//This is how you would change the variable in Testing2 class
Testing2.NewIntYAY = 5;
}
}
See how I put Testing2 in front of the variable that I wanted to use, that is how you modify that variable in another class. If it isn't working it is probably because your variable isn't static, it has to be static to access it in another class. Also, try using the Update() function instead of making your own if you want something to be checked constantly. Here is your codes fixed:
using UnityEngine;
using System.Collections;
public class wfa2 : MonoBehaviour{
public float bullets = 7f;
public GameObject deagle;
public bool animationreloadplayed = false;
public bool fired = false;
public float Distance;
public float MaxDistance = 100;
public float Damage = 25;
public bool Reloading = false;
public bool timedown;
public float time = .75f;
public bool firedshot = false;
public static bool haswaited = false;
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Input.GetButtonDown ("Fire1") && animationreloadplayed == false)
{
firedshot = true;
fired = true;
}
if (bullets <= 0f && bullets < 7)
{
animationreloadplayed = true;
}
if (fired == true)
{
RaycastHit hit;
{
if(Physics.Raycast (transform.position, transform.TransformDirection(Vector3.down), out hit))
{
Distance = hit.distance;
if (Distance < MaxDistance){
hit.transform.SendMessage ("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
Debug.DrawLine(transform.position, hit.point, Color.red);
fired = false;
}
}
}
}
if (Input.GetKeyDown (KeyCode.R))
{
animationreloadplayed = true;
}
if (animationreloadplayed == true)
{
deagle.animation.Play ("Reload");
bullets = 7f;
if (bullets == 7 && animationreloadplayed == true)
{
animationreloadplayed = false;
}
}
if (firedshot == true)
{
Debug.Log("Fired Shot!");
Force.IsTiming = true;
if (haswaited == true)
{
bullets = bullets -1f;
deagle.animation.Play ("Fire");
firedshot = false;
audio.Play ();
haswaited = false;
Debug.Log("I have waited!");
}
}
}
}
Next Code!
using UnityEngine;
using System.Collections;
public class Force : MonoBehaviour
{
public static bool IsTiming = false;
public float NumberOfSecondsToWait = 1f;
public float TotalTime = 0f;
void Update()
{
if(IsTiming == true)
{
TotalTime += Time.deltaTime;
Debug.Log ("Timing!");
}
if (TotalTime >= NumberOfSecondsToWait)
{
wfa2.haswaited = true;
IsTiming = false;
TotalTime = 0f;
}
}
}
If you have any problems or this didn't achieve what you wanted just add a comments, and I will help you modify the code to your needs. :D. [1]: http://www.youtube.com/watch?v=ZwxMlIS6TLM
Answer by Crumpet · Apr 02, 2014 at 12:57 PM
first of all sorry for the late reply I was studying for exams
Okay, so I understand now what I had to do. I had tried this method but I didn't realise what the "static" meant. I do now anyway so I understand how this all works but I'm having an issue where I can fire while the reloading animation is playing causing the reloading animation to freeze the gun halfway out. So basically the slider is still back and the clip is still out of the gun. I tried to use this timer to build a similar way around this by doing this. I commented things out next to it to show what I was trying to do. anyway here.
First script using UnityEngine; using System.Collections;
public class wfa2 : MonoBehaviour
{
public float bullets = 7f;
public GameObject deagle;
public bool animationreloadplayed = false;
public bool fired = false;
public float Distance;
public float MaxDistance = 100;
public float Damage = 25;
public bool Reloading = false;
public bool timedown;
public float time = .84f;
public bool firedshot = false;
public static bool haswaited = false;
public static bool haswaited2 = false;
public static bool Reloadinggun = false;
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Input.GetButtonDown ("Fire1") && Reloadinggun == false)// && Reloadinggun == false was added so that if it is true then this should not work meaning when the timer is working I shouldn't be able to fire?
{
firedshot = true;
fired = true;
}
if (bullets <= 0f && bullets < 7)
{
animationreloadplayed = true;
Force2.IsTiming2 = true;
}
if (fired == true)
{
RaycastHit hit;
{
if(Physics.Raycast (transform.position, transform.TransformDirection(Vector3.down), out hit) && haswaited == true)
{
Distance = hit.distance;
if (Distance < MaxDistance)
{
hit.transform.SendMessage ("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
Debug.DrawLine(transform.position, hit.point, Color.red);
fired = false;
}
}
}
}
if (Input.GetKeyDown (KeyCode.R))
{
animationreloadplayed = true;
Force2.IsTiming2 = true;
}
if (animationreloadplayed == true && haswaited2 == true)
{
deagle.animation.Play ("Reload");
bullets = 7f;
Force2.IsTiming2 = false;
if (bullets == 7)
{
animationreloadplayed = false;
}
}
if (firedshot == true && animationreloadplayed == false)
{
Debug.Log("Fired Shot!");
Force.IsTiming = true;
if (haswaited == true)
{
bullets = bullets -1f;
deagle.animation.Play ("Fire");
firedshot = false;
audio.Play ();
haswaited = false;
Debug.Log("I have waited!");
}
}
}
}
Second script
using UnityEngine;
using System.Collections;
public class Force2 : MonoBehaviour
{
public float NumberOfSecondsToWait = 2f;
public float TotalTime = 0f;
public GameObject deagle;
public static bool IsTiming2 = false;
void Update()
{
if(IsTiming2 == true)
{
wfa2.Reloadinggun = true;//Now the timer has started, this is true meaning I should not be able to fire because if (Input.GetButtonDown ("Fire1") && Reloadinggun == false)
TotalTime += Time.deltaTime;
Debug.Log ("Timing2!");
}
if (TotalTime >= NumberOfSecondsToWait)
{
wfa2.haswaited2 = true;
IsTiming2 = false;
TotalTime = 0f;
wfa2.Reloadinggun = false;//after waiting this is false again so the gun should be able to fire now?
}
}
}
I tried to explain this the best I can here is an image to hopefully help you understand the gun is halfway through the reload animation so when it fires it gets caught here and you have a gun frozen like this. Thank you so much for your help by the way you did solve my question I was just hoping you could help me with this before I mark it as solved
If you could give me a little more information please, is this when you have ammo and you are trying to reload or when you are out of ammo? If it is getting stuck while you try to shoot when it is reloading you can add a bool that will prevent it from firing.
I think I understand what your problem is, the waiting lasts for 2 seconds so if your gun reloading animation lasts for more than 2 seconds it should let you fire. Although I am still a little confused I will try to help as much as I can!
Try adding the Reloadinggun = true; in your push r or out of bullets functions such as this:
if (bullets <= 0f && bullets < 7)
{
animationreloadplayed = true;
Force2.IsTi$$anonymous$$g2 = true;
Realodinggun = true;
}
Then also here:
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.R))
{
animationreloadplayed = true;
Force2.IsTi$$anonymous$$g2 = true;
Realodinggun = true;
}
I hope that helps if it doesn't please tell me what is wrong.
$$anonymous$$y gun reloading animation goes for less than two seconds. You're about as confused as I am. If I add Reloadinggun = true; to where you say then I can't fire after the reload, but no way can I implement a way to make the bool false again without have the issue above. You have the right idea. When I am shooting away and press R or I go below 1 bullet the gun will reload and then when I click the gun plays the firing animation over the top of the reloading one causing the reloading one to freeze like in my previous posts picture.
Ok I will try testing this myself, sorry for this issue. I will try to solve this as soon as I can.
Your answer
Follow this Question
Related Questions
How do I get Single ammo reloading to work? 0 Answers
Firing Sound 2 Answers
Firing Cooler 1 Answer
Bullets going wrong direction 1 Answer
Gun Not working on Web Player 1 Answer