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 CatDoge · Mar 15, 2015 at 07:02 PM · javascript

Assigning script to a variable with a variable

So the title is a bit weird but i didnt know how to call it.

I am working on a FPS game and I am trying to make a simple weaponsystem. Every Player can have a primary and a secondary weapon. I am at the moment trying to write a script to change between the assigned primary/secondary weapons.

So at first I am doing this:

 var primary : GameObject;
 var secondary : GameObject;

So I have some GUI Buttons that when they get clicked they assign the desired weapon to the variables primary/secondary.

An code example:

 function assignump45() {
 primary = ump;
 }

Now I want to write a function to switch between the primary and secondary weapon. So I tried this:

 function switchtoprimary(){
 if(Input.GetKeyDown("2")){
 
 primary.inv(); //makes the primary weapon invisible
 secondary.vis(); //makes the primary weapon visible
 
 }
 }


Of course I get this error:

BCE0019: 'inv' is not a member of 'UnityEngine.GameObject'.

I know that what I wrote is wrong. So I tried to get the script of the primary/secondary weapons so I can disable/activate them:

 var primscipt : umpscript = GameObject.Find(ump).GetComponent(umpscript);

This works BUT I can´t write for every weapon this kind of script because I then I need to write several combinations of switching between the weapons and that isn´t possible because i know there is a better solution..

I can´t do a if clause and then assign the primscript because the variable only would be assigned in the if clause..

What I need is something like this (doesn´t work of course^^).

 var primscipt : primaryscriptstring = GameObject.Find(primarystring).GetComponent(primaryscriptstring);

So I could assign the variable primaryscriptstring with "umpscript" for example. the variable primarystring does work in this case

Are there any workarounds? I am pretty desperate at the moment :/

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
0

Answer by siaran · Mar 15, 2015 at 08:44 PM

I think you're overthinking this. Why not something simple like

 //put all weapon gameobjects in this array
 GameObject[] weapons;
 //pointer to currently active weapon
 int activeWeaponIndex;
 
 void SwitchToWeapon(int newWeaponIndex){
   weapons[activeWeaponIndex].SetActive(false);
   activeWeaponIndex = newWeaponIndex;
  weapons[activeWeaponIndex].SetActive(true);
 }

If you need to access more of your weapon's functionality, make a Weapon[] instead of a GameObject[], then you could do things like weapons[i].SomeWeaponFunction();

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Functions overwrite or work seperatelly ? 1 Answer

How to declare typed array in UnityScript 1 Answer

My script seems fine, yet I keep getting BCE0044: expecting }, found ''. 1 Answer

How to I play an animation using the Animation Element Array via Script? 0 Answers

BCE0053: Property 'UnityEngine.Component.light' is read only. 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