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 Natanijel · Dec 01, 2012 at 02:19 PM · instantiatepublicaccessing

Problem: Accessing Other Script Variables

Hello :)

There is a small problem I have when I try to access a public variable from another script.

I have a Ghost prefab, and it's children are the left and right arm. When the Ghost is instantiated, it's children (the arms) produce results for some variables and the parent (the ghost) is mean to read them. However this is not happening.

Here is part of the arm script:

 public var forward_left: boolean;
 public var visible_left: boolean;
 
 function Start () {
 
 }
 
 function Update () {
 
     if(Physics.Raycast(transform.position, 
                   (GameObject.Find("MainChar").transform.position - transform.position).normalized,
                    Vector3.Distance(transform.position, GameObject.Find("MainChar").transform.position)-1)){
                            visible_left = false;
                    }else{visible_left = true;} 
     
     if(Physics.Raycast(transform.position, transform.forward, 1)){
         forward_left = true;
     } else{forward_left = false;}
     
 }

This is part of my ghost script:

 if(GameObject.Find("left").GetComponent(ARM_left).forward_left == true){do_something;}


Am I doing something wrong? Does "GameObject.Find" NOT look for children?

Thanks in advance :D Muhasaresa ---||---

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

3 Replies

· Add your reply
  • Sort: 
avatar image
-1

Answer by valaxgames · Dec 01, 2012 at 05:19 PM

You need to add 'static' before the variable you want to access. For example:

 static var health : int = 100;

To access it:

 //Will print the health variable in the other script.
 Debug.Log(ScriptName.health);

Hope I helped!

Comment
Add comment · Show 1 · 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 Eric5h5 · Dec 01, 2012 at 05:36 PM 0
Share

You do not need to add "static", and should not unless you actually mean for there to be one instance of that variable.

avatar image
-1

Answer by neogrant2 · Dec 01, 2012 at 05:19 PM

Assuming your "Arm script" is called ArmScript.js,

Change:

 static var forward_left : boolean = false;
 static var visible_left : boolean = false;


In your Ghost script:

 if(ArmScript.forward_left){
     print("True");
     
 }
Comment
Add comment · Show 1 · 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 Eric5h5 · Dec 01, 2012 at 05:39 PM 0
Share

Don't use static except for specific circumstances. It's not a substitute for GetComponent.

avatar image
0

Answer by Eric5h5 · Dec 01, 2012 at 05:38 PM

GameObject.Find does find children, however you should use Transform.Find.

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

13 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

Related Questions

Accessing variable from another script 1 Answer

Enemy Health Damage 2 Answers

Cloning Problem Script 0 Answers

Accessing not yet publicly added scirpt 3 Answers

Platform Mover continued 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