- Home /
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 ?
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.
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 :)
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.
Answer by Kiwasi · Jun 29, 2014 at 10:05 AM
Multiple solutions
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
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
Separate it out into different scripts and attach each one as a separate component
Write completely different scripts for each object
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.
seems inheritence is the only way
Not even remotely. It's probably the worse option.
Answer by flaviusxvii · Jun 28, 2014 at 03:50 PM
5 different script for just a simple difference in direction ? seems annoying
Variables.
sure i know about variables :) see the new answer i posted, it makes the problem more obvious.
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.
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.
nope, no problem with the arrays. see the new answer i posted. i described it again.
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.
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
![](https://koobas.hobune.stream/wayback/20220613150828im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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