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 MiniFish · Dec 19, 2012 at 06:18 PM · objectaccess

A way to set object's script as a variable of sorts?

I have some background in C# programming, but am a newbie of sorts with Unity

I'll get to the main point, I'm currently working on a mecha game where you can swap out weapon parts on the player character, I've got everything nailed down, except that swapping weapons part.

All of the weapons are objects with scripts, instantiated and parented to the PC's weapon point on game start, but I'm kinda lost as I need to fire the weapons and different weapons have different script.

So, is there anyway that I can grab the object's script and use it like a variable?

Not sure if I'm painting a clear picture but here's the code sample :

     public class PlayerActionscript : MonoBehaviour 
     {
         PlayerRocketLauncher mWeapon1;
         public GameObject mWeapon_R_U;
     
         // Use this for initialization
         void Start () 
         {
             mWeapon1 = mWeapon_R_L.GetComponent<PlayerRocketLauncher>();
         }
         
         // Update is called once per frame
         void Update () 
         {
             if(mIsFiring){mWeapon1.mIsFiring = true;}
         }


Another way that I think would work is that every time the script updates I'll make a check to see what sort of weapon it is then activate the shooting script, something like this :

         public GameObject mWeapon_R_U;
 
         if(mWeapon_R_U.name.Contains("RocketLauncher"))
         {
         mWeapon_R_U.GetComponent<PlayerRocketLauncher>().mIsFiring = true;
         }

So, yeah, is there some way to grab an object's script and use it like a variable or I'm stuck with using the messy method?

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
Best Answer

Answer by cagezero · Dec 19, 2012 at 07:52 PM

Sounds like you need to use either an abstract class or an interface for the weapon variable. Each weapon (like your Rocket Launcher) should then either extend or implement that abstract class or interface and the PlayerActionscript won't have to worry about what type of weapon it is referencing, it can just fire it.

Here is an example of the abstract class way:

 public class PlayerActionscript : MonoBehaviour  {
     
     //Might be a RocketLauncher, might be a Pistol. PlayerActionscript does not care.
     Weapon mWeapon1;
     
     // Use this for initialization
     void Start () 
     {
        mWeapon1 = GetComponent<Weapon>();
     }
 
     // Update is called once per frame
     void Update () 
     {
        if(mWeapon1.IsFiring()){mWeapon1.SetIsFiring(true);}
     }
 }
 
 public abstract class Weapon : MonoBehaviour {
     
     //A variable visible to all subclasses
     protected bool isFiring;
     
     //Functions with the same implementation for all Weapons
     public bool IsFiring() {
         return isFiring;
     }
     public void SetIsFiring(bool firing) {
         isFiring = firing;
     }
     
     //Functions with the same implementation for most weapons
     public virtual void Reload() {
         
     }
     
     //Functions requiring custom implementations by all Weapons
     public abstract void Fire();
 }
 
 public class RocketLauncher : Weapon {
     
     //A variable only visible to the rocketlauncher
     GameObject rocket;
     
     //Reloading a RocketLauncher probably takes a little longer. Override to customize
     public override void Reload() {
         
     }
     
     //All weapons have to provide cutom firing code
     public override void Fire() {
         
         
     }
     
     //Put some private RocketLauncher functions here.
 }

  

[removed vague weapon switching comment. not sure why it was there in the first place @_@]

Hope that helps.

Comment
Add comment · Show 4 · 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 MiniFish · Dec 20, 2012 at 11:28 AM 0
Share

Wow, that's something new for me. I think I get what you mean, but I'm still kind of confused, because my rocket launcher actually have a script that does its own thing, but from what I see here is that I need to move the rocket launcher script into another class in PlayerActionScript?

avatar image cagezero · Dec 20, 2012 at 05:51 PM 0
Share

edited my answer for clarity and removed the comment about weapon switching.

avatar image cagezero · Dec 20, 2012 at 05:53 PM 0
Share

Without seeing the rest of your code and project setup I can only speak to best practices. In general, you will want as little dependency between the PlayerActionscript and the actions it controls. So, no. I do not think moving the RocketLauncher script into the PlayerActionscript is necessary. For every action you need a Weapon to have, add a function to the Weapon class and have the subclass implement it.

I am not sure what your level of comfort is with different oop paradigms, but it seemed like these links might be useful.

http://answers.unity3d.com/questions/233859/whats-the-use-of-abstract-classes-and-interfaces-i.html

http://msdn.microsoft.com/en-us/library/scsyfw1d%28v=vs.71%29.aspx

http://stackoverflow.com/questions/56867/interface-vs-base-class

avatar image MiniFish · Dec 20, 2012 at 09:17 PM 0
Share

aye, that did clear up lots of stuff. thanks for the help.

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

10 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

Related Questions

Access object from script problem. 1 Answer

Same script not working on all objects 1 Answer

Object doesn't stick on Camera right Corner?? (Basic Question) 1 Answer

Get GameObject name from other object. 2 Answers

Drag Script Not Working 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