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 /
avatar image
1
Question by GameMasterShift · Mar 22, 2017 at 07:19 AM · gameobjectclassesreference-other-object

I have everything in the right place but why wont my value stay changed ?

So i have my scripts below and what is supposed to happen is a timer counts down them spawns a beam then restarted the counter and then if the beam is still spawned just restart the counter without spawning a beam then once the beam is collected by the player the value is checked and the beam is supposed to be able to be spawned again but for some reason when it comes to that the value is changing when it is called but after that goes back to normal so whats happening here ?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 
 public class BlueTracker : MonoBehaviour {
 
     public GameObject Beam;
 
     public float BlueTimer;
     float Due;
     public int isBeamSpawn;
 
     // Use this for initialization 
     void Start () {
 
         Due = Random.Range (10, 15);
         BlueTimer = Due;        
     }
     
     // Update is called once per frame
     void Update () {
             
             BlueTimer -= Time.deltaTime;
 
 
         if (BlueTimer <= 0 && isBeamSpawn == 0) {
             Instantiate (Beam, transform.position, transform.rotation);
             Due = Random.Range (10, 15);
             BlueTimer = Due;
             isBeamSpawn = 1;
         } else if (BlueTimer <= 0 && isBeamSpawn == 1) {
             Due = Random.Range (10, 15);
             BlueTimer = Due;
         }
     }
 
     public void BeamCheck(int spawned)
     {
         isBeamSpawn -= spawned;
     }
 }

my other script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
     
     public class Collect : MonoBehaviour {
     
         public GameObject Sign;
     
         public BlueTracker blue;
     
         // Use this for initialization
         void Awake () {
     
             blue = Sign.GetComponent<BlueTracker> ();
         }
         
         // Update is called once per frame
         void Update () {
     
              
     
         }
     
         void OnTriggerStay(Collider other)
         {
             if (Input.GetKeyDown (KeyCode.E)) {
                 blue.BeamCheck(1);
                 Destroy (gameObject);        
             }
         }
     }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by peter_kes · Mar 24, 2017 at 12:17 PM

"Input.GetKeyDown () " You need to call this function from the Update function, since the state gets reset each frame..." does this give you some hint? I am curious if Input.GetKeyDown () works in triggerstay.

Comment
Add comment · Show 2 · 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 GameMasterShift · Mar 24, 2017 at 11:53 PM 0
Share

I changed it around and even tried a few false variables, but if I do a "Print" for the isBeamSpawn it changes once the E is pressed but then goes back to 1 ins$$anonymous$$d of staying at 0

avatar image PSpiroz GameMasterShift · Mar 25, 2017 at 02:21 PM 0
Share
          if (BlueTimer <= 0 && isBeamSpawn == 0) { //<-------IF "isBeam.."==0
              Instantiate (Beam, transform.position, transform.rotation);
              Due = Random.Range (10, 15);
              BlueTimer = Due;
              isBeamSpawn = 1; //<--------- "isBeam.." becomes 1

This happens in every frame.

When do you want it to be 1 and when to stay 0?

If isBeanSpawn is always 1 or 0 why don't you make it boolean? $$anonymous$$aybe you'll see thingsmore clear. Run a scholastic Debug. Put in every single line a print referring the line and isBeanSpawn value. See step by step what happens first in the code order.

avatar image
0

Answer by PSpiroz · Mar 26, 2017 at 05:15 PM

Not sure what your saying. Please use punctuation in your sentences. What do you mean by "back to normal?" what do you want to happen and what happens exactly. Beware of what you are "Destroy" and what action / method runs first in every script.

You can also try make a method this area of script:

 void doThis(){
          if (BlueTimer <= 0 && isBeamSpawn == 0) {
              Instantiate (Beam, transform.position, transform.rotation);
              Due = Random.Range (10, 15);
              BlueTimer = Due;
              isBeamSpawn = 1;
          } else if (BlueTimer <= 0 && isBeamSpawn == 1) {
              Due = Random.Range (10, 15);
              BlueTimer = Due;
          }
 }

And call it both in 'Update()' function and in 'BeamCheck( int spawned)'

see how it goes...

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Classes. After creating objects. How do I find my objects by class rather than their objects? 2 Answers

Can't re-enable a gameobject 1 Answer

Count returning 0 when called outside object~ 1 Answer

Need help with assigning game objects to variable 2 Answers

GameObject1's Bool is FALSE when GameObject2 is Active? 2 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