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
1
Question by SC4V4NGER · Dec 15, 2013 at 07:14 PM · c#scriptingbasicsboolean

Get Boolean from another Script, C#

I have already tried to ask this question befor, but let me try aggain:

I want to know an easy way to use a boolean that is toggled in "Script1" in "Script2" (in c#)

This Question was also already asked a 100 times by other people but I just don't seem to understand...It would be great if someone could help me,

Thank you

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 Statement · Dec 15, 2013 at 10:53 PM 0
Share

Not in C# but it should be easy to convert the three lines of code: http://codeparalysis.blogspot.co.uk/2013/12/how-to-access-variable-in-script-from.html

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by YoungDeveloper · Dec 15, 2013 at 07:20 PM

Hey there, here is a detailed explanation how access stuff from other script! If you still dont understand specific detailed, sure ask.

http://answers.unity3d.com/questions/550578/cant-understand-getcomponent-c.html

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 SC4V4NGER · Dec 15, 2013 at 07:39 PM 0
Share

Thank You that made some things clear :) But still I seem to unskilled to fix my problem on my own...

I want a door to open when my Player (just a ball) collides with a "switch". $$anonymous$$y Player is taged "$$anonymous$$ugel"

The Switch script:

public class Switch : $$anonymous$$onoBehaviour {

 public bool tuerAuf; 


 // Use this for initialization
 void Start () {
     tuerAuf = false;
 }
 
 // Update is called once per frame
 void Update () {
 


 }

 void OnCollisionEnter (Collision collision) //=======================================================================================================================================
 {

     if (collision.gameObject.tag == "$$anonymous$$ugel") 
     {

         tuerAuf = true;

     }
     
     




 }

}

The Door Script:

public class TuerSwitch : $$anonymous$$onoBehaviour { private Vector3 startPos; public bool auf;

 public GameObject CollisionCube;
 Switch switchScript;
 
 
 // Use this for initialization
 void Start () {

     startPos = transform.position;
     auf = false;

     switchScript = CollisionCube.GetComponent<Switch> ();
     auf = switchScript.tuerAuf;
 }
 
 // Update is called once per frame
 void Update () 
 {

     




     if(auf) 
     {
         transform.Translate (0, 0, 3 * Time.deltaTime);  
         if (transform.position.z > startPos.z + 1)
             auf = false;
     } 

     
     
 }

}

It would be really great if you could help me solve my Problem :D The door just won't move

avatar image YoungDeveloper · Dec 15, 2013 at 07:56 PM 0
Share

Ins$$anonymous$$d of checking the collision on door, you should do it on the player ins$$anonymous$$d, because what if you will have 10 doors, it means there will 10 on collision checks each frame. But as it only will be on player, there will be only one oncollision check.

Still is all what you need on player:

 void OnCollisionEnter(Collision col){
     if (col.gameObject.tag == "Door"){
         Switch switchScript = col.gameObject.GetComponent<Switch>(); //Get the Switch script from collision gameobject col
         switchScript.OpenDoor(); //runs the function to open door
     }
 }
 
 
 This should be on door: (You must have tag "Door" set)
 
 public class Switch : $$anonymous$$onoBehaviour {
 
     private bool open = false; //boolean to trigger opening
 
     public void OpenDoor(){
         open = true;
     }
 
     void Update(){
         if(open){
             //open door code
         }
     }
 }
avatar image SC4V4NGER · Dec 15, 2013 at 08:14 PM 0
Share

I think I now have the exact same code implemented, but it still doesn't work... I am getting slightly frustrated over this

avatar image SC4V4NGER · Dec 15, 2013 at 08:17 PM 0
Share

I just recreated the Object with "Door" on it, and now it works :) Thanks a lot you made my day

avatar image
4

Answer by Spinnernicholas · Dec 15, 2013 at 07:32 PM

If they are on the same gameobject:

 //in script1
 public boolean toggle
 
 //in script2
 if(getComponent<script1>().toggle)
 {
 
 }

If they are not on the same GameObject:

 //in script1
 public boolean toggle
 
 //in script2
 //Set this to the other script in the Unity Editor
 public script1 otherScript
 //...
 if(otherScript.toggle)
 {
 
 }
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 Spinnernicholas · Dec 15, 2013 at 07:34 PM 0
Share

There are many other ways if they are on different gameObjects.

avatar image SC4V4NGER · Dec 15, 2013 at 07:50 PM 0
Share

I might just be to stupid, but it still doesn't work... I now get NullReferenceException: Object reference not set to an instance of an object

avatar image SC4V4NGER · Dec 15, 2013 at 07:52 PM 0
Share

In script 2 it now looks like this:

if (otherScript.toggle)

                     auf = true;

     if(auf) 
     {
         transform.Translate (0, 0, 3 * Time.deltaTime);  
         if (transform.position.z > startPos.z + 1)
             auf = false;
     } 
avatar image Spinnernicholas · Dec 15, 2013 at 10:48 PM 0
Share

In the editor, select the gameobject with script 2 in the hierarchy. Then, in the inspector, set otherScript to the other script.

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

20 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

Related Questions

Check bool from another script and use in another script C# 3 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Unable to show gameobjects at specific timing 0 Answers

How to get multiple string values from override ToString()? 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