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 /
avatar image
1
Question by frmusta · Mar 31, 2010 at 05:41 PM · variablereferenceguistylebce0020

GUI.Style referencing, one script to another

Is it possible to reference a GUI.Style i made in one Script to another? If so what is the correct way to do this.

I have tried setting my original GUI.Style variable to public:

public var myStyle :GUIStyle;

and referencing this style from my other script like this:

GUI.Box (Rect (5, 150, 100, 400), "Hello", script01.myStyle);

But get an error: "Assets/GUI/Bar_Bottom.js(42,76): BCE0020: An instance of type 'script01' is required to access non static member 'myStyle'."

If i change the variable to public static var, it crashes unity. Is this just not possible and i have to recreate the same style in each script? Or just make a GUISkin even though im only changing the look of one item.

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

Answer by Michael La Voie · Mar 31, 2010 at 05:54 PM

If the script you want to access is a component of the same game object as the script that will access it, you can load up the script using GetComponent()

otherscript = GetComponent(MyOtherScriptName); 
GUI.Box (Rect (5, 150, 100, 400), "Hello", otherscript.myStyle);

Alternatively, if the script is part of another game object, first find that game object, then use GetComponent() to get its script

otherGameObject = GameObject.Find("OtherGameObjectName");
otherscript = otherGameObject.GetComponent(MyOtherScriptName); 
GUI.Box (Rect (5, 150, 100, 400), "Hello", otherscript.myStyle);

As a third option, if you know the other game object at design time, you can create a public variable to hold it in the script that will do the accessing. Then, from Unity's property inspector, you can drag and drop the other game object onto that variable.

var otherGameObject : GameObject;
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

No one has followed this question yet.

Related Questions

Can I make reference/assocation to another component as variable? 0 Answers

Use Of Static Variables 1 Answer

Accessing a variable of a script from another scene. 7 Answers

How to call a function from another script - getting error "Object reference not set to an instance of an object" 5 Answers

Change value of integer based on string input in a separate script 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