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 kylehlogan · Sep 30, 2012 at 03:24 PM · colorvariablesreferencing

Properly referencing a variable from another script

Having a bad day in regards to coding - I'm having problems understanding how to properly reference variables from other scripts, here's the script I want to reference.

 #pragma strict
 var lightcolour : Color[];
 public var selection;
 var firstchange = 60;
 var changerate = 60;
 
 function Update () 
 {
     if(Time.time > changerate)
     {
         changerate = Time.time + firstchange;
         var chance = Random.Range(1,10);
     }
     
     if(chance == 1)
     {
         selection = lightcolour [Random.Range(0, lightcolour.length)]; 
         light.color = selection;
     }
 }

This script works perfectly, but I want to be able to reference the variable 'selection' in another script and use it to change the GUI.color property of a GUI texture so that the colour of the GUI texture is the same as the light.

I'm just not entirely sure how I do this, I've read the documentation but I don't understand how to reference a variable from a script.

Thanks.

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 whydoidoit · Sep 30, 2012 at 03:25 PM 0
Share

Firstly we need to know how the scripts are related:

  • Always related

  • Related due to a collision

  • On the same object

  • Some other way?

avatar image kylehlogan · Sep 30, 2012 at 03:47 PM 0
Share

One of the scripts it attached to a directional light, the other to a GUI texture. They should always share that variable so if A changes, B also changes.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by whydoidoit · Sep 30, 2012 at 03:50 PM

So you need to relate the two scripts together:

Create a variable of type WhatEverTheScriptYouPostedIsCalled in the script attached to the texture.

  var lightScript : WhatEverTheScriptYouPostedIsCalled;

Then use the inspector to drop the light object onto the variable called lightScript.

Then use lightScript.selection in your code on the texture.

Comment
Add comment · Show 8 · 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 kylehlogan · Sep 30, 2012 at 04:08 PM 0
Share

GUI.color = lightScript.selection;

Gives me an 'Object reference not set to an instance of an object' error, I've assigned the light object in the inspector so I'm not sure what's wrong.

avatar image whydoidoit · Sep 30, 2012 at 04:10 PM 0
Share

Doesn't sound like it took then.

Before that line do an if(!lightScript) Debug.Log("Value not set");

avatar image whydoidoit · Sep 30, 2012 at 04:10 PM 0
Share

Ah you should define selection as

  var selection : Color;

I'm guessing that this isn't set yet on the first call.

avatar image kylehlogan · Sep 30, 2012 at 04:20 PM 0
Share

Already did that, the colour isn't changing in the inspector for the GUI texture. I added the debug line and it's not doing anything, still receiving the same error as before.

avatar image whydoidoit · Sep 30, 2012 at 04:54 PM 0
Share

Hang on - shouldn't you be changing:

   GetComponent(GUITexture).color = lightScript.selection
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

11 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

Related Questions

Color variable matching 2 Answers

How can I reference a float variable to a GUI label in another script? 1 Answer

How to make Unitron recognise and correctly color variables and integers etc? 1 Answer

Instance(?) problems 1 Answer

How to share a variable through multiple copys of the same script 2 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