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 Thejuster · Jan 08, 2013 at 12:03 PM · objectpubliccallingmember

Activate public bool from other script

Hello i have a problem to activate a public bool member in other script file in GameObject.

I Have a gameobject called "Elevatore" (Elevator)

whit this script and public bool member

http://img838.imageshack.us/img838/7158/objws1.png

Elevat Script

Discesa [ ] (Bool member) Salita [ ] (Bool member)


now in other object whit an'other script i need to enabile this bool

example

void OnTriggerEnter(Collider object) {

object.Discesa = true;

}

How to?

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 AlucardJay · Jan 08, 2013 at 12:24 PM 1
Share

check fafases answer here : http://answers.unity3d.com/questions/368362/accessing-variables-from-different-scripts.html

check Fatties answer here : http://answers.unity3d.com/questions/332250/accessing-other-gameobjects-script-variables.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DanielJF · Jan 08, 2013 at 12:36 PM

I will try to explain the basics because your question is kinda confusing but i hope this helps.

 class TestFile extends MonoBehaviour
 {
     static var main : TestFile;
     var Discesa : boolean = true;
     
     function Awake()
     {
         main = this;
     }
 }

in this first class i create a boolean called Discesa and it's true in the begin. Now i will make a second script and put the boolean to false.

 class testfile2 extends MonoBehaviour
 {
 
     function Start () 
     {
         TestFile.main.Discesa = false;
         Debug.Log(TestFile.main.Discesa);
     }
 }

by creating a var called main in the first script it will be able to find this script. the thing you do first is type the name off the class then main so the class knows you mean this script. Now you are in the script and you can acces all functions and variables etc.. //script1 using UnityEngine; using System.Collections;

 public class TestFile1 : MonoBehaviour {
 
     public bool Discesa = true;
     public static TestFile1 main;
     
     void Awake () 
     {
         main = this;    
     }
 }

//script 2

 using UnityEngine;
 using System.Collections;
 
 public class TestFile2 : MonoBehaviour {
 
     // Use this for initialization
     void Start () 
     {
         TestFile1.main.Discesa = false;
     }
 }

Hope it helped.

Comment
Add comment · Show 7 · 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 DanielJF · Jan 08, 2013 at 01:08 PM 0
Share

i just see you program in c# i will update my post in a sec for c# im not that well with it but i think i can fix it

avatar image Thejuster · Jan 08, 2013 at 01:43 PM 0
Share

tanks $$anonymous$$ :)

Your code i think is good boy for my situation does work. i now have try this

void OnTriggerEnter(Collider coll) {

coll.gameObject.GetComponent().discesa=true;

}

Unity say: NullReferenceException: Object reference not set to an instance of an object pulsante.OnTriggerEnter (UnityEngine.Collider coll) (at Assets/pulsante.cs:24)

:/

avatar image DanielJF · Jan 08, 2013 at 01:50 PM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class TestFile1 : $$anonymous$$onoBehaviour {
 
     public bool Discesa = true;
     public static TestFile1 main;
     
     void Awake () 
     {
         main = this;    
     }
 }

 using UnityEngine;
 using System.Collections;
 
 public class TestFile2 : $$anonymous$$onoBehaviour {
 
     public bool Test;
     void Start () 
     {
         Test = gameObject.GetComponent<TestFile1>();
         TestFile1.main.Discesa = false;
     }
 }

$$anonymous$$aybe this helps for you.

avatar image Thejuster · Jan 08, 2013 at 02:14 PM 0
Share

i Have follow your example but nothing error. here my script:

File: pulsante.cs

This script is placed on a Cube this cube is an Activator. If player touch this cube. this script enable a bool contains in other script for running elevator. called elvat.cs

using UnityEngine; using System.Collections;

public class pulsante : $$anonymous$$onoBehaviour {

 // Use this for initialization
 void Start () 
 {
 
 }
 
 
 void OnTriggerEnter(Collider coll)
 {
     print ("touch");

 var Test = gameObject.GetComponent();
     Test.main.main.Discesa = false;
 }
 
 
 // Update is called once per frame
 void Update () {
 
 }

}

This script is placed on simple cube for create a elevator on a object. if player touch the object the script need to turn on a bool discesa or salita true | false.


File Elvat.cs

This is a Elevator script. if discesa bool is enabled, The script move a gameobject containts this script up or down. salita = up discesa = down

using UnityEngine; using System.Collections;

public class elvat : $$anonymous$$onoBehaviour {

 public static elvat main;
 // Use this for initialization
 void Awake () 
 {
 main = this;
     
 }
 
 // Update is called once per frame
 public bool discesa = false;
 public bool  salita = false;
 
 void Update () {
     
     if(discesa)
     {
 float velocity = 1f;
     float TimedeltaTime = Time.deltaTime;
     transform.position = new Vector3(transform.position.x,transform.position.y - velocity * TimedeltaTime,transform.position.z);
             
     if(transform.position.y >= 70)velocity = -5;
     else if(transform.position.y <= 40)velocity = 10;
     }
     
     
     if(salita)
     {
 float velocity = 1f;
     float TimedeltaTime = Time.deltaTime;
     transform.position = new Vector3(transform.position.x,transform.position.y + velocity * TimedeltaTime,transform.position.z);
             
     if(transform.position.y >= 70)velocity = +5;
     else if(transform.position.y <= 40)velocity = 10;
     }
 }
 
 
 void OnTriggerEnter(Collider other)
 {
     
     
 }
 
 

}

Who are the problem?

avatar image Thejuster · Jan 08, 2013 at 02:47 PM 0
Share

Fixed Sorry

GameObject obj = GameObject.FindWithTag ("Elevatore"); print (obj.transform.position.x.ToString()); obj.GetComponent().discesa=true;

Show more comments

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

10 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

Related Questions

Same script not working on all objects 1 Answer

Object doesn't stick on Camera right Corner?? (Basic Question) 1 Answer

Use via Inspector to script assigned public texture as default 0 Answers

Drag Script Not Working 1 Answer

How to draw GUI Text from code 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