Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by n-carlson1 · May 23, 2016 at 01:30 AM · timerreferenceenum

How to change the value of a timer from another script?

So I have a timer in my script that sets the interval between shots for the boss i've made. The problem is, when it's called from another script it shoots automatically and then resets the timer (I have an enumeration that randomly chooses a state). My guess is it's kind of a simple fix to just reference the timer from the other script and reset the timer, but I don't know how to reference it. I can't define it as static because then it completely breaks so I need to reference it some other way. I tried it the GetComponent way, but I couldn't seem to figure it out. Here is all the relevant code.

 public class BossSpecial : MonoBehaviour
 {
 public GameObject bulletPrefab;
 public static bool fire = false;
 public static int shootCounter = 0;
 public float specialTimer = 1f;
 void Update()
 {
     specialTimer -= Time.deltaTime;
     if (specialTimer <= 0)
     if (BossScript.bossActionsValue == BossScript.BossActions.Special) 
     {
         Shoot();
     }
 }
 public void Shoot()
 {
         Vector3 offset = transform.rotation * new Vector3(0, 0, 0);
         Instantiate(bulletPrefab, transform.position + offset, transform.rotation);
         shootCounter++
         specialTimer = 1f;
         fire = false;
 }

That's the script that I need the timer to change in.

Here is the script I need the timer to be changed from.

 public class BossScript : MonoBehaviour
 {
 void Special()
 {
     BossSpecial.fire = true;
     bossActionsValue = BossActions.Special;
     speed = 5f;
     currentWayPoint = 8;
     if ((targetWayPoint == null) || (currentWayPoint == 8))
         targetWayPoint = wayPointList[currentWayPoint];
     transform.position = Vector3.MoveTowards(transform.position, targetWayPoint.position, speed * Time.deltaTime);
     if (BossSpecial.shootCounter >= 39) //shoots 8 ways so if we want it to shoot 5 times we have to do (8 * 5) - 1
     {  
         CurrentState = BossActions.MoveToSpot1;
     }
 }

I tried to use something like GameObject.Find("Boss").GetComponent().specialTimer = 1f;

But that didn't work. This is still a pretty rough bit of code that I have a ton to work on. I'm fairly certain the shootCounter doesn't reset either but i'll figure that out. The Special() method though is called from a switch statement inside the Update() method in the BossScript script.

Last, but not least, I don't know if it matters but the BossScript is attached to the Boss and the BossSpecial is attached to a child gameobject of the boss to determine where the shot originates from.

Hopefully that's enough to work with to get the gist of what i'm trying to do.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Arsineh · May 25, 2016 at 03:16 PM

@n-carlson1

There are lots of answers in Unity answers, here are 2 of them, and you can find more similar answers link1 and link2

Good luck

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Arsineh · May 25, 2016 at 03:17 PM 0
Share

Personally I used Static variable for passing variable from one script to another

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

56 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Score isn't able to be counted 0 Answers

Using the 'is' operator versus an abstract enum in identifying a child class. 0 Answers

Repeated coundown timer 0 Answers

How to call an enum from another script with an enum 2 Answers

Using Direction enum to move an enemy from off screen 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges