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 dansav · Mar 20, 2012 at 09:07 PM · variableglobalpropertypragma

how can I create a variable to refer to any gameObject script with pragma strict

I want to create a global variable in pragma strict that refers to the script attached to any game object. Is this possible? scriptTypes are script1,script2,script3 etc...

  #pragma strict;
     var script; //Defaults to Object;
     
 
 function GetProperty(go:GameObject,scriptType:?){
     script=go.GetComponent(scriptType);
     Debug.Log(script.property1);
     }
Comment
Add comment · Show 5
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 Julien-Lynge · Mar 20, 2012 at 09:13 PM 0
Share

Am I understanding this correctly that you want a variable that can hold a script1, script2, script3, etc., and from what your code says, access properties that are shared between these scripts?

If that's the case, what I'd do in C# is derive all your scripts from a base class and then set the variable to the base type.

e.g., the base class is myClass, and the definition for script1 is

class script1 : myClass

then you'd define your variable as so:

var script : myClass

Any properties you'd want to share between the scripts would need to be defined in the base class and overridden if desired in the derived classes.

Unfortunately, I have no idea how to do this in JS. Hopefully someone else can answer that for you.

avatar image syclamoth · Mar 20, 2012 at 09:32 PM 0
Share

That'd be

 class script1 extends myClass{}

in JS.

avatar image dansav · Mar 20, 2012 at 09:49 PM 0
Share

Are you saying in javascript I would write

class scriptType{ var script1:script1; var script2:script2; }

var script:scriptType; I'm confused about how this would help.

avatar image dansav · Mar 20, 2012 at 09:55 PM 0
Share

syclamoth: can scriptType be a variable that I can pass into a function? So I could do.

function getProperty(go:GameObject,scriptType:$$anonymous$$onobehavior){

script=go.GetComponent(scriptType);

script(as scriptType).property=10;

}

or something like that...?

avatar image syclamoth · Mar 20, 2012 at 11:12 PM 0
Share

Well, even if that were possible (which it is, in C#, but we'll get to that later if you want to), you'd still have the same problem- You wouldn't be able to know what the exact type that was inputted into the function was!

So, say you have a type 'scriptType' that includes the variable 'property', and another type called 'anotherType' that does not- there's no way to know that your hypothetical function 'getProperty' has been called like this:

 getProperty(myObject, scriptType);

ins$$anonymous$$d of like this:

 getProperty(myObject, anotherType);

So, you come back to the same problem. You need to specifically limit the number of types that you can use, if you want to be able to access a specific variable.

The best way to do this, if you were using C#, would be to use a common interface. This way, you could implement it however you liked, as long as the script conformed to a given 'signature'- and then you could use that to access the variables ins$$anonymous$$d.

But you're using JS, so I can't really help you.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by syclamoth · Mar 20, 2012 at 09:33 PM

There are two things that you're trying to do here, and they kind of conflict.

If you have a variable that you want to be able to refer to any script, it's simple enough to just go-

 var script : MonoBehaviour;

But, this won't allow you to access

 script.property1

because that is specific to a certain subclass. Because at that point in the program, that object could be anything, there's no way that the compiler can know exactly what type you expect there, so it won't allow you to use it that way.

There are a few ways around this. The simplest is, if you (the programmer) know what the type is expected to be, to just manually cast the object into the correct type.

 script(as scriptType).property;

Keep in mind that this won't work if you get it wrong.

The other way you could go would be with @NOAA_Julien's solution, but this puts a hard limit on the types that you can use with the system- so you have to be aware of the limitation. Also keep in mind that using this architecture can screw around with the serialization- which isn't a problem if it only needs to work at runtime, but make sure it doesn't catch you unaware.

Comment
Add comment · Show 6 · 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 dansav · Mar 21, 2012 at 01:17 AM 0
Share

var script:$$anonymous$$onoBehaviour;

Is there a javascript equivalent of this line:

script(as scriptType).property;

or for typecasting script below as a cubescript?

script= GameObject.Find("Cube").GetComponent(cubescript);

avatar image syclamoth · Mar 21, 2012 at 01:26 AM 0
Share

I think the generic version should work properly-

 var script : cubescript = GameObject.Find("Cube").GetComponent.<cubescript>();

that or

 var script : cubescript = GameObject.Find("Cube").GetComponent(cubescript) as cubescript;
avatar image dansav · Mar 21, 2012 at 04:15 AM 0
Share

I'm running into trouble. I'm trying to set the script once globally depending on the object -- so that all other functions can use the script.

var script:$$anonymous$$onoBehaviour; function getScript(name,type){ if ( name=="cube"); var script:cubescript= GameObject.Find("cube").GetComponent(cubescript) as cubescript;
}

function setProperty(){ getScript("cube","cubescript"); script.property1=20; Debug.Log(script.property1); } setProperty();

avatar image dansav · Mar 21, 2012 at 04:17 AM 0
Share

also how are you highlighting the code in a comment. I couldn't find any info about how to do that...

avatar image syclamoth · Mar 21, 2012 at 05:05 AM 0
Share

For starters, I was talking hypothetically- I'm pretty sure you can't do what you're trying to do here. Also, for the code highlighting, I'm taking advantage of the fact that there's no functional difference beetween the syntax in the 'answer' field and the syntax in the 'comment' field- for long segments it's as easy as leaving one empty line on each side, and four spaces before each line, and for short segments use the tags and < /code > (without the spaces, but obviously I can't write that) to mark individual words and sentences (you see what I did there?).

If it's all too hard, you can also write everything out in the 'answer' field, use the preview to make sure it looks right, and then cut and paste it into the comment box!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unable to modify a variable in another script 2 Answers

Global Varible Problem 2 Answers

How do I find the name and value for each variable of a script component, using Unity script? 2 Answers

Howto access global var from other script? 2 Answers

How to delete a Variable in a script in game 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