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 MasterGalaxy · Jun 05, 2012 at 10:19 PM · monobehaviourassignment

Script as a variable being assigned later in script???

I am writing a script, currently it is getting a little big in my opinion (around 500 lines of code)

It was hard to make, so I won't post it here. My question is... I am trying to use GetComponent to get a variable from within another script. The problem is, I want to be able to change which script to get it from using the script as a variable... Example

 `var script : ???;
 gameObject.GetComponent(script).variable
 if(2 == 2){
    script = otherscript
 }`


See my dilemma? I don't know what type to use for my variable. I thought MonoBehavior, but it throws tons of errors! It would say "variable is not a member of UnityEngine.Component" So can someone help me out, please?

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 Berenger · Jun 05, 2012 at 10:24 PM 0
Share

Do you want to be able to change your code quickly to run tests ? Because I would suggest ctrl+h.

avatar image MasterGalaxy · Jun 05, 2012 at 10:33 PM 0
Share

Actually, my script is going on an NPC that will detect another NPCs particular AI script, and adjust it's actions accordingly.

3 Replies

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

Answer by MasterGalaxy · Jun 08, 2012 at 12:26 AM

I had #Pragma Strict on. Without it, everything works fine. I feel like an idiot now. THANKS EVERYONE FOR ALL OF YOUR HELP!!!

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 bompi88 · Jun 08, 2012 at 09:13 AM 1
Share

Of course ;-) I should have thought of that :S Sorry.

avatar image
0

Answer by bompi88 · Jun 05, 2012 at 11:43 PM

Does the script variable containing the script have to be "global"? Or could you do something like this?

 function yourFunction (whichScriptToUse : int) : float {
     var theScript;
     
     switch(whichScriptToUse) {
         case 0:
             theScript = gameObject.GetComponent(script);
             break;
         case 1:
             theScript = gameObject.GetComponent(otherscript);
             break;
     }
     
     return theScript.variable;
 }
 
 function Start () {
     // using script2s variable
     var variableFromScript : float;
     variableFromScript = yourFunction(0);
     Debug.Log("Variable in Script1: " + variableFromScript);
     variableFromScript = yourFunction(1);
     Debug.Log("Variable in Script2: " + variableFromScript);
 }
Comment
Add comment · Show 9 · 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 MasterGalaxy · Jun 06, 2012 at 09:36 PM 0
Share

I like this idea, it could work. I haven't tried it yet, but when I get a chance, I surely will! thank you for this.

avatar image MasterGalaxy · Jun 07, 2012 at 10:32 PM 0
Share

I tried implementing this in my javascript/unityscript, but it didn't work. Unfortunately, I'm not sure if my problem is fixable in javascript. If it is, please, I would love to know how.

avatar image bompi88 · Jun 07, 2012 at 10:37 PM 0
Share

How did it not work? Did you get errors or did it not do what you want it to do?

avatar image MasterGalaxy · Jun 07, 2012 at 10:48 PM 0
Share

I got plenty of errors. Apparently, it's reading the variable script as a string, and for some reason, getcomponent is rejecting it.

BCE0004: Ambiguous reference 'GetComponent': UnityEngine.GameObject.GetComponent(String), UnityEngine.GameObject.GetComponent(System.Type).

avatar image bompi88 · Jun 07, 2012 at 11:03 PM 0
Share

What are the name of your script? script.js and otherscript.js? Then try my edited code above.

Show more comments
avatar image
0

Answer by Berenger · Jun 05, 2012 at 11:55 PM

I suggest you create an abstract class inherithing from MonoBehaviour class and two (or more) classes inheriting from it. In the script in your question, deal with the abstract class and implement the particular behaviour inside the children class. If it's not clear, tell me and I will add an example.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

cant add a script because of monobehaviour 1 Answer

Is there a "Lightweight Monobehaviour"? 1 Answer

Scripting Errors I cannot fix, Could use a helping hand and someone to tell me how to fix it. 2 Answers

Getting into Monobehavior 1 Answer

How can I reference a specific script inside a GameObject from another MonoBehaviour. 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