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 iRaMb0 · Dec 23, 2012 at 07:24 PM · variables

Accessing variables from different scripts.

I know this has been asked multiple times but nothing has worked for me so far...i tried GetComponent but i got errors, i used these lines :
var options: Options = GetComponent(Options);

options.WaitTime;

What i'm trying to do is access the WaitTime variable from the "Options" script so i can use it with the "Play" script.

my code so far (regarding the WaitTime variable)on the "Play" script is this :

  var waitTime: Options = GetComponent(Options); 
      waitTime.WaitTime;
 
 
     
     
     
  
 
 
   
   
   
  
   
   
 
 function wait(){
 
 yield WaitForSeconds(waitTime);
 lol = false;
 
 
 
 
 (i call the wait function through "OnMouseExit")

AND ON MY OPTIONS SCRIPT IT IS THIS:

 static  var WaitTime : int;
 
 
 
 
 
 
 function wait(){
 
 yield WaitForSeconds(WaitTime);
 lol = false;
 
 
 
 
 }
 
 
 
 function Update(){
 if( WaitTime < 0){
 
      WaitTime= 0;
    
 }
 
 if( WaitTime >60){
 
      WaitTime= 60;
    
 }
 
 
 
 }
 (Again, i call the wait function on "OnMouseExit")

In my "Options" script i have the variable WaitTime and i want this variable to have the same value on both scripts ( and a 3rd one later on ).When i use those scripts i get 2 errors though:

1.Assets/MyScripts/Play.js(75,21): BCE0024: The type 'UnityEngine.WaitForSeconds' does not have a visible constructor that matches the argument list '(Options)'.

2.Assets/MyScripts/Play.js(4,15): BCE0034: Expressions in statements must only be executed for their side-effects.

I don't know if i was clear to what i wanted but, thanks in advance!!!

Comment
Add comment · Show 3
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 PushpaK · Jan 04, 2013 at 09:07 AM 0
Share

here yield WaitForSeconds(waitTime); you are passing gameobject where it is expecting float. Pass float value e.g waitTime.WaitTime

avatar image Theinsanekiller · Jan 04, 2013 at 09:32 AM 0
Share

Hey, You have to use static variable to use in multiple scripts. For that you have to initialize a variable with "static" keyword. And now in play script u have to use like this: className.variableName in other class Option.WaitForSeconds=0; like this.

avatar image iRaMb0 · Jan 04, 2013 at 06:07 PM 0
Share

it worked with pushpak's and Glauco's replies combined, thank you

3 Replies

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

Answer by iRaMb0 · Jan 04, 2013 at 07:28 PM

Thank you guys for your replies , i solved the problem :D

in the pIay script i put these Iines:

  var waitTime: Options = GetComponent(Options); 
 
 //a bunch of code here
 
 
   function wait(){
 
   yield WaitForSeconds(waitTime.WaitTime);
   lol = false;
 
 
 
 
 }
 
 

plus...i used what Gauco Beini Ramos Filho mentioned as well.

So thank you all, for making my day

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 MountDoomTeam · Jan 04, 2013 at 08:20 PM 0
Share

$$anonymous$$ark the question was answered then

avatar image
1

Answer by fafase · Jan 04, 2013 at 08:17 PM

You know this questions has many answers on Unity Answers or even Unity Forum or even articles here http://unitygems.com/

One thing to keep in mind, avoid static even though you have been told to. This is not because it would not work here but more likely because you seem not to know what they are for yet and they would probably make you think they are for this purpose when they are not.

As for now, you have a target script and a fetcher script.

Target.js

 var targetVar: AnyType;

Fetcher.js

 var scriptReference : Target;
 
 function Start(){
     // If script on different object
     scriptReference = GameObject.Find("ObjectName").GetComponent(Target);
     // If script on same object
     scriptReference = gameObject.GetComponent(Target);
 }
 
 function KungFoo(){
     scriptReference.targetVar = value;
 }

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
avatar image
0

Answer by gbelini · Jan 04, 2013 at 10:48 AM

Hey, try this:

 public static var WaitTime : int;
Comment
Add comment · Show 2 · 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 iRaMb0 · Jan 04, 2013 at 05:35 PM 0
Share

it doesn't work...i get the same errors

avatar image Eric5h5 · Jan 04, 2013 at 07:09 PM 0
Share

You should not use static unless you specifically mean for there to be only one instance. It's not a substitute for GetComponent.

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

14 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

Related Questions

Multiple Cars not working 1 Answer

modifying scripts for mobile 0 Answers

Help Me with my gun! 1 Answer

How do you return variables from other scripts? 2 Answers

Hover Script problem 0 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