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
-1
Question by VoidScreamer · Jun 05, 2014 at 09:23 AM · class objectobject-oriented-programming

Every active Object a Script? no some efficient way?

I have 5 Objects Patrolling same, but in various directions . the simple way is to create 5 different scripts and attach them one by one to objects. I mean 5 different script for just a simple difference in direction ? seems annoying.

is there an easier, Efficinet way to make 1 script and customize it for specific objects?

something like Inheritence in class programming ?

and another question. where i should define a public class, to derive from it in scripts ? in another script ?

Comment
Add comment · Show 4
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 fafase · Jun 05, 2014 at 09:23 AM 2
Share

Your solution lies in some beginner tutorials.

avatar image VoidScreamer · Jun 28, 2014 at 02:52 PM 0
Share

actually there's no way. but we can in example, define some variables to set them independent in inspector.that's the only way, that one script can be used for 2 solution.

avatar image VoidScreamer · Jun 29, 2014 at 09:54 AM -1
Share

Let me make it easier. I have 5 Different Box Collider. and a script the script covers these actions :

1) checks for other colliders entering the attached gameobjects 2) turns on the red light (for example) 3) turns off the green light.

Now :

--I want the first Box Collider to do these things. so i simple attach the script to it.

  • I want the second Box Collider to just turn Off the green light. Action 1 and Action 2 in script arent needed. now i have to delete the lines corresponding to actions 1 and 2. but if i remove this lines, the whole script changes and every other object (like Box Collider 1) changes. that's the problem. beacuse of this, i have to make a new script for Box Collider 2. and then, i have to make a lot of scripts just varying in few lines.

in other word, i want to make a General Script , import it to an object and customize it just for that object. so i have 1 script in my scripts folder, but many Various Objects behaving customized based on that general script.

Exactly like what we do, when we're inherting a child class from another class. i want to to this for scripts :)

avatar image VoidScreamer · Jun 29, 2014 at 03:19 PM 0
Share

maybe there's some problems in describing the problem, but no spam$$anonymous$$g.

and the answer is that there's no way to do that in way i want.

4 Replies

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

Answer by Kiwasi · Jun 29, 2014 at 10:05 AM

Multiple solutions

  1. Use variables. You can use a public bool that is checked before each action to determine weather to take it or not. Set the bool via an inspector or another script

  2. Use inheritance. Both JavaScript and C# support regular inheritance. Anything that inherits from MonoBehavior can be attached as a component, even if there are several other inheritance layers in between

  3. Separate it out into different scripts and attach each one as a separate component

  4. Write completely different scripts for each object

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 VoidScreamer · Jun 29, 2014 at 02:47 PM 0
Share

seems inheritence is the only way

avatar image Jeff-Kesselman · Jun 29, 2014 at 04:09 PM 0
Share

Only because you came in the door thinking that.

avatar image Kiwasi · Jun 29, 2014 at 08:38 PM 0
Share

It really all depends on your application. I use all of the methods above. There is also another more advanced technique I failed to mention, which is using custom classes as plugins to change functionality.

avatar image flaviusxvii · Jul 04, 2014 at 03:30 PM 0
Share

seems inheritence is the only way

Not even remotely. It's probably the worse option.

avatar image
1

Answer by flaviusxvii · Jun 28, 2014 at 03:50 PM

5 different script for just a simple difference in direction ? seems annoying

Variables.

Comment
Add comment · Show 3 · 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 VoidScreamer · Jun 29, 2014 at 09:09 AM 0
Share

sure i know about variables :) see the new answer i posted, it makes the problem more obvious.

avatar image Kiwasi · Jun 29, 2014 at 10:06 AM 0
Share

Variables is still a valid solution...

avatar image flaviusxvii · Jun 29, 2014 at 01:47 PM 0
Share

You should strive to make each piece of code you write as simple and clear as possible. Having several scripts that perform different tasks makes perfect sense to me.

If you REALLY want one script that performs multiple functions, you can declare several boolean variables 'lightsRed', 'lightsGreen' and then use if(lightsRed...) in your collision handler to decide what this cube does.

avatar image
0

Answer by DimitriUK · Jun 28, 2014 at 03:08 PM

Hi, I'm not too sure how new you are, but I believe what you are looking for is an array.. So whichever object you are using to apply the script, you should look in to your script and change the var, as an example:

public var Patrols : GameObject[]; //This allows an array, check the inspector to apply them

public var Patrols : GameObject; //This is not an array, and is just an individual Game Object.

Let me know if this helps.

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 VoidScreamer · Jun 29, 2014 at 09:08 AM 0
Share

nope, no problem with the arrays. see the new answer i posted. i described it again.

avatar image
0

Answer by Jeff-Kesselman · Jun 29, 2014 at 02:49 PM

I think you are not understanding what people are telling you

The proper unity solution is public fields. Every public field gets exposed in the Inspector and can be set differently for every different instance of that component on an object. For instance you could have

 public int direction;


Where 0= north, 1 = east, etc...

Or velocities for movement in X and Y. (Which RigidBody already exposes for you)

Or anything else you want to set per object.

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 VoidScreamer · Jun 29, 2014 at 03:16 PM 0
Share

feeling same.

i have no problem with vars,Arrays,internal arrays or something. im just curios about finding some way to decrease scripts as possible. as i said in the long comment above.

im closing this question due to misunderstanding.

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

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

Related Questions

C# finding gameobject that created this class, and do I need to clear them? 1 Answer

How do I check if a derived class is a certain class? 1 Answer

How do I check if a derived class is a certain class? 0 Answers

[CLOSED] How do I check if a derived class is a certain class? 0 Answers

Please, Where´s the error? c# 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