Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Acquial · Feb 01, 2014 at 09:02 AM · scriptingbasicsbooleanstatic-vars

Can read but cannot pass variables between scripts...

Hey. So I have a public bool assigned to the public bool from a script connected with another gameObject. I want to change the boolean value of the remote script when my raycast encounters an "enemy" tag. After testing I've determined that I am getting the true value of BattleTime.On() and that my raycasting is working as intended, but I cannot alter the value of BattleTime.On(). Here is my player script:

     public bool battletime;
         public void Update() {
             
             battletime = GameObject.Find("Manager").GetComponent<BattleTime>().on;
 
         RaycastHit hitInfo1;
         Ray ray = new Ray (transform.position, new Vector3(0,0,-1));
         if (Physics.Raycast (ray, out hitInfo1, Mathf.Infinity)) {
             if (hitInfo1.collider.gameObject.CompareTag ("enemy")) {
                 //Physics2D.Raycast (ray1, out hitInfo1, Mathf.Infinity)) {
                 battletime = true;
                 Debug.Log (battletime);
             }} else {
             battletime = false;
             Debug.Log (battletime);
         }
 
 
 
     And here is the constant script, located in the Manager gameObject:
 
 
 
         
     public bool on;
             void Update(){
             if (on == true)
                             Debug.Log ("it is true");
                     else
                             Debug.Log ("lies");
     
         }

Turning 'on' into a static script does not help. Anyone got any ideas?

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
Best Answer

Answer by getyour411 · Feb 01, 2014 at 09:04 AM

This is how I do it. Instead of this

 battletime = GameObject.Find("Manager").GetComponent<BattleTime>().on;

I do

 battletime = GameObject.Find("Manager").GetComponent<BattleTime>();

and then instead of

 battletime

I do

 battletime.on == ....

I haven't tried the method you are using with the direct reference, not saying it's wrong, just the method that I Try this site too: http://unitygems.com/script-interaction1/

Comment
Add comment · Show 4 · 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 Acquial · Feb 01, 2014 at 03:23 PM 0
Share

Thanks for the info, but it still isn't working. Also, I'm trying to alter the value of BattleTime().on, not compare it to anything. Should I just make the battletime formulation local to move and then have other objects constantly reference it...?

Let me try explaining it again.

The move script looks for raycasthits on update. It can distinguish between an 'enemy' tag and anything else. That works The battletime bool successfully references GameObject.$$anonymous$$anager().on, I have verified this by having the move script produce output corresponding to BattleTime().on and altering on's value directly in the inspector. That works

BUT If I want a condition met in the move script to alter the value of BattleTime().on, it won't work. I have read the manual on script interaction, and the only thing I haven't tried is converting BattleTime().on to a static variable, which I -thought- was sufficient-but-not-necessary. God this is irritating.

I've got a move script, which includes:

 public bool battletime;
        public void Update() {
  
        battletime = GameObject.Find("$$anonymous$$anager").GetComponent<BattleTime>().on;
  
        RaycastHit hitInfo1;
        Ray ray = new Ray (transform.position, new Vector3(0,0,-1));
        if (Physics.Raycast (ray, out hitInfo1, $$anonymous$$athf.Infinity)) {
          if (hitInfo1.collider.gameObject.CompareTag ("enemy")) {
          battletime = true;
           Debug.Log (battletime);}}
          else {
          battletime = false;
          Debug.Log (battletime);
        }

And my $$anonymous$$anager object has a BattleTime script, which contains:

     public bool on;
     void Update(){
     if (on == true)
                 Debug.Log ("it is true");
     if (on == false) 
                     Debug.Log ("lies");
 }
avatar image robertbu · Feb 01, 2014 at 03:27 PM 0
Share

@getyour411 answer is very close. Some variables are by value, so are by reference. Boolean variables are by value, so you are getting a copy of your variable. I believe you want:

 public BattleTime battletime;
         public void Update() {
  
            battletime = GameObject.Find("$$anonymous$$anager").GetComponent<BattleTime>();
  
        RaycastHit hitInfo1;
        Ray ray = new Ray (transform.position, new Vector3(0,0,-1));
        if (Physics.Raycast (ray, out hitInfo1, $$anonymous$$athf.Infinity)) {
          if (hitInfo1.collider.gameObject.CompareTag ("enemy")) {
           //Physics2D.Raycast (ray1, out hitInfo1, $$anonymous$$athf.Infinity)) {
           battletime.on = true;
           Debug.Log (battletime);
          }} else {
          battletime.on = false;
          Debug.Log (battletime);
        }

Note how 'battletime' is of type 'BattleTime' not bool.

avatar image Acquial · Feb 01, 2014 at 04:54 PM 0
Share

Thanks a lot, both of you. That worked. Gonna have to cogitate for a little while.

avatar image robertbu · Feb 01, 2014 at 05:00 PM 0
Share

@Acquial - If your question is answered, click on the checkmark next to the left of the answer to close it out. Thanks.

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

18 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

Related Questions

Get Boolean from another Script, C# 2 Answers

How to smooth between values? 2 Answers

How do I change the logged text based on a momentary collision? 1 Answer

Script throwing error Operator '==' cannot be used with a left hand side of type System.Object and right hand side of type Controller 1 Answer

Toggle between first and third person - how do I toggle back? 1 Answer


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