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 Desprez · Jan 29, 2015 at 02:24 PM · referencetypecacheoptimisationtypecast

Change what script a var holds?

I'm trying to use a single variable to refer to different scripts, depending on what object is using it.

The basic set-up is: The turret, holds a weapon, which holds an ammo type.

The turret and weapon scripts need to reference the ammo script held by the weapon, and a weapon variable will define what ammo type is used (such as bullet or missile). These type differ radically and really can't be efficiently combined.

However, the weapon scrips share much functionality, and really can't be split into separate scripts for each type - just for the sake of using a different variable to reference the ammo.

Now despite the vast differences between ammo behavior, they DO share a number of common variables that need to be referenced often, hence the problem.

(The referencing scripts used to be separate, but the code was becoming unmanageable and at this point in development they need to be cleaned up.)

 var _script : System.Object;
 
 // typically in Start()
 if (condition) {
     _script = gameObject.GetComponent(Bullet);
 } else {
     _script = gameObject.GetComponent(Missile);
 }

 // so I can easily reference the cached component 
 _damage = _script.damage;
 _range = _script.range;
 // etc.

So how do I go about this?

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 gjf · Jan 29, 2015 at 11:20 AM

look up inheritance. i don't know the unityscript syntax.

create a base class for the weapons containing the common variables and also store the weapon type then when you check which component to get, do so by referencing the type.

in your posted code where you check condition, check the type before getting the relevant component - which is only needed for things not in the base class.

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 meat5000 ♦ · Jan 29, 2015 at 12:52 PM 0
Share

I believe every class displays inheritance as they extend $$anonymous$$onobehaviour by default.

You can simply change what your class extends from.

 public class BlockStruct extends System.ValueType (uJS)

For example.

avatar image Desprez · Jan 29, 2015 at 09:43 PM 0
Share

I'm not sure what you mean exactly.

Starting with the turret object, it has a Turret script. Inside that, there is a var for which weapon object the turret holds. That weapon object has it's own Weapon script. Within that script, I have a var for what ammo the weapon holds. The ammo object will eventually become the game object that get shots on screen, and has its own script, and these script are different depending on type of ammo.

How does the turret or gun reference something in the ammo script?

Normally, to access a script you have to type the reference var according to the script name. Since the script names vary, I'm a little unsure how to proceed.

 var _script : Bullet
 _script = gameObject.GetComponent(Bullet);
 // however, I can't then use script to access $$anonymous$$issile
 _script = gameObject.GetComponent($$anonymous$$issile); // no good


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

Cannot cast from source type to destination type 2 Answers

Error CS0425: Works in DLL but not in Unity 0 Answers

How can you store a reference to something that you don't know the type of yet? 2 Answers

Vector3 is a primitive type in Unity? 1 Answer

Referencing a c# component script from JS script 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