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 Johnsons · Sep 13, 2017 at 11:32 PM · variablevariablesstaticaccessing from any scriptaccessing scripts

Change of static variable

Hello. I have a little problem, ive got a script, lets say Script_1, in which there is a while loop, that is running as long as the player is alive. I am accessing bool variable for the while loop from another script, Script_2. In Script_2, in which the variable is declared, i need to change the value of the variable on impact, i.e. the player dies on impact.

As i have found on other threads, i can access the variable, if it is public static bool variable.

And in deed, i accessed this variable in the Script_1 like this:

 //Script_2
 public static bool alive = true;

 //Script_1
 bool playeralive = Script_2.alive;

The problem is, that when there is an actual impact, and i have in Script_2:

 void OnTriggerEnter(Collider other)
     {
         if (other.tag == "Asteroid")
             alive = false;
     }

The variable has not changed, because if it did, the game would end itself. This is the loop in Script_1:

 bool playeralive = Script_2.alive;
 while (playeralive)
         {
             for (int i = 0; i < hazardcount; i++)
             {
                 Vector3 spawnpos = new Vector3(sth,sth,sth);
                 Quaternion spawnrot = Quaternion.identity;
 
                 Instantiate(hazard, spawnpos, spawnrot);
             }
             hazardcount = hazardcount + 5;
         }
         Application.Quit();

I am very sorry, if this is completely wrong, just started a few days ago. I would appreciate any advice, thanks.

Comment
Add comment · Show 1
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 Hellium · Sep 25, 2017 at 04:08 PM 0
Share

You must do while (Script_2.alive) ins$$anonymous$$d of while (playeralive)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by GianBert · Sep 25, 2017 at 03:19 PM

You must write in script 1

 public bool playeralive;

 Script_2 script_2;

     void Awake()
     {
     script_2 = GetComponent<Script_2>();
     }

and after you can write

 void Update()
 {
      if(script_2.alive == true)
      {
            playeralive = true;
      }
      else
      {
            playeralive = false;
       }
 }


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

121 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Calling variables from other script 1 Answer

Why UNITY Hates Me By Reading Array's Length! 1 Answer

how to controll a variable of a script from another script 2 Answers

How to get all the variables in a folder. C# 0 Answers

How to send a variable from one script to another when the script finishes. 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