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 beyluta · Mar 16, 2016 at 01:12 PM · script.booleanfloatmultiplereferencing

How to set float, bool from one script to the other?

I know how to make reference from another script and how to use it's functions but I am trying to set a bool using the script I made reference to. The script that contains those variables is the other one and the bool and / or floats should change there too, but it isn't. Is it suppose to work this way? is there a way to change on both?

  __________________________________________OriginalScript_________________________________________
 void Update () {
 
         if (DoesItEvenWork == true) {

             Debug.Log ("It works!");
   }
  __________________________________________OriginalScript_________________________________________

  __________________________________________Reference Script______________________________________
  if (Input.GetKey ("e")) {

             ScriptReference.DoesItEvenWork = true;

         }
   __________________________________________Reference Script______________________________________
Comment
Add comment · Show 2
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 ComradeVanti · Mar 16, 2016 at 05:41 PM 0
Share

Sorry but your code-sample here is all over the place... We dont know in witch script the variable DoesItEvenWork is declared... so yeah. Please use actual code samples next time.

If you want to change the variable of another script you will have to make the variable public. But it seems like you have already done that correctly. Is your OriginalScript attached to an active gameobject? Update in only called on active objects. Also try debugging DoesItEvenWork in ReferenceScript so you see if the value has actually changed.

avatar image beyluta ComradeVanti · Mar 16, 2016 at 06:23 PM 0
Share

All my variables are public and all gameobjects are active. So this did not helped much.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Guhanesh · Mar 17, 2016 at 06:34 AM

 using UnityEngine;
 using System.Collections;
 
 public class OriginalScript : MonoBehaviour {
         public bool DoesItEvenWork=false;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
                 if (DoesItEvenWork == true) {
                         Debug.Log ("It works!");
                 }
     }
 }

Attach the above script to a gameobject and rename the gameobject to GameObject.

 using UnityEngine;
 using System.Collections;
 
 public class referenceScript : MonoBehaviour {
     OriginalScript orignalSctipt;
     // Use this for initialization
     void Start () {
         orignalSctipt=GameObject.Find("GameObject").GetComponent<OriginalScript>();
         
     
     }
     
     // Update is called once per frame
     void Update () {
                 if (Input.GetKey ("e")) {
                         orignalSctipt.DoesItEvenWork=true;
                 }
     }
 }

Attach the above script to another gameobject in the scene.And play and check.

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 beyluta · Mar 18, 2016 at 03:53 PM 0
Share

Nope, nothing happend

avatar image Soraphis · Mar 18, 2016 at 04:53 PM 1
Share

in referenceScript, line 8 should be:

 originalScript = (OriginalScript ) FindObjectOfType(typeof(OriginalScript));

or the object has to be named "GameObject" (and being the first one, else GetComponent will result null)

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

53 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

Related Questions

how to switch in multiple camera's? 0 Answers

Operator (less than sign) cannot be used with a left hand side of type boolean and a right hand side of type float 1 Answer

error CS0266 Cannot implicitly convert type `float' to `int'. An explicit conversion exists (are you missing a cast?) 2 Answers

Same float for the same buttons. 0 Answers

I need to move multiple objects to a position multiple of 0.16. But it doesnt work. 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