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 Finnark · Mar 21, 2014 at 07:36 AM · c#component

declaring unknown script components under a empty variable

in this code: using UnityEngine; using System.Collections;

 public class example : MonoBehaviour {
     void Update() {
         OtherScript otherScript = GetComponent<OtherScript>();
         otherScript.DoSomething();
     }
 }

is there a way so that i can declare a empty variable for the OtherScript part? for example what would be the appropriate variable type in this code?:

 using UnityEngine;
 using System.Collections;
 
 public class example : MonoBehaviour {
 public /*variable type?*/ otherScript;
     void Start() {
         OtherScript otherScript = GetComponent<OtherScript>();
     }
     void Update() {
         otherScript.DoSomething();
     }
 }
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 vexe · Mar 21, 2014 at 07:55 AM 0
Share

it's just OtherScript

 public class example : $$anonymous$$onoBehaviour {
     public OtherScript otherScript;
     void Start() {
         otherScript = GetComponent<OtherScript>();
     }
     void Update() {
         otherScript.DoSomething();
     }
 }
avatar image Finnark · Mar 25, 2014 at 07:09 AM 0
Share

what if the OtherScript might apply to different scripts under different circumstances? for example, OtherScript might have to be a script called ScriptA while other times, it might have to read from ScriptB or even ScriptC?

avatar image ShadoX · Mar 25, 2014 at 07:23 AM 0
Share

Have you tried "Component"?

 Component otherScript = GetComponent<OtherScript>();

I'm just assu$$anonymous$$g that if this works, you'd probably have to specify the script type manually each time you try to call a method/function...

 otherScript<movement>.walk(); //just an example


http://docs.unity3d.com/Documentation/ScriptReference/Component.html

You could try reflection, but I'm not sure how great that would be performance wise..

What exactly are you trying to do? Perhaps ther's a simpler solution to your problem.

Like using a interface or inheritance

avatar image Finnark · Mar 25, 2014 at 07:31 AM 0
Share

im trying to make it so that i can use teh same damage calculation function script for multiple types of bullets colliding with multiple types of enemies. so basically, i want it so that i can use the same script when bullet 1 hits enemy A as well as when bullet 1 hits enemy B, bullet 2 hits enemy A, etc. and returns the damage dealt after the script reads the current hp of the enemy, the enemys armor value, etc.

avatar image ShadoX · Mar 25, 2014 at 11:53 AM 0
Share

so basically all you need are 2 scripts, 1 on the bullet, 1 on the enemy with just different settings for the variables, correct?

so something like (just an example):

 class Bullet {
    int damage = 3;//some default value
 }
 
 class enemyHP {
    int hp = 100;
 
    public void doDamage(int dmg) {
       hp -= dmg;
       if(hp < 0) { 
          //enemy dies...
       }
    }
 }

you'd put those on each enemy and bullet and just change the Bullet.damage and the enemyHP.doDamage(..) function would take care of calculating how much damage the enemy receives..

and on the bullet you'd probably also want to check for the collision and apply the damage.. so something like

 void onCollisionEnter(Collision col){
    if(col.collider.getComponent<enemyHP> != null){
       col.collider.getComponent<enemyHP>.doDamage(Bullet.damage);
    }
 }

hope you get the idea.. there are obviously other ways to do it , but this would be 1 way of handling it

0 Replies

· Add your reply
  • Sort: 

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to trigger udpate in editor mode only when I modify component parameters 1 Answer

I'm having trouble getting the text in Unity's "text mesh" to change its text at runtime. 1 Answer

How to disable script in other object 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