I currently have code like so:
#pragma strict
function OnTriggerExit(collider : Collider)
{
if (collider.tag == "Respawn")
{
if (PaddleRightBehaviorScript.goalCounter > 0)
{
--PaddleRightBehaviorScript.goalCounter;
}
if (PaddleRightBehaviorScript.specialMeter > 0)
{
--PaddleRightBehaviorScript.specialMeter;
}
}
}
function Update()
{
if (PaddleRightBehaviorScript.goalCounter == 0)
{
Destroy(gameObject);
}
}
But I have this same exact code in four different scripts, each referencing a different script (PaddleLeftBehaviorScript, and so on.) This is quite tedious and I’m sure there’s a way to condense this code into a single file, but I’m not sure how to do that. Any help?
EDIT: This script is attached to an empty gameobject that detects when a ball passes a line.
Scripts can talk to each other via SendMessage command, or if you make a variable static then you can use scriptname.varname = foo to change or read variables in different scripts.
The latter is what I’m doing, but the problem is that I don’t know how to reference different scripts based on what object the current script is attached to.
//This is untested
While I’m not sure what your trying to do, could you not do the following?
//Whatever script this is referencing. I'm not sure how to type it so I'll simply let the compiler (or you) do it.
var script;
function OnTriggerExit(collider : Collider)
{
if (collider.tag == "Respawn")
{
if (script.goalCounter > 0) --script.goalCounter;
if (script.specialMeter > 0) --script.specialMeter;
}
}
function Update()
{
if (script.goalCounter == 0) Destroy(gameObject);
}
I’d also move the test in the Update() to something not called every frame. A ‘Set’ property (again, don’t know how to do so in JS) or simply in OnTriggerExit if that’s the only way to lose goalCounter.
Now you have one script you can D&D onto relevant GO’s.
I’ve tried that but it won’t show up in the inspector without a type, and each script has a different name. Also, I’ll look into what you said about moving the code out of Update(). It didn’t work when I tried it before, but I think I have a different idea of how to do that now.
If each script does the same, or very similar things, why are you giving them different names?
Have you tried giving them the type ‘Component’?
I’m having similar problems with those scripts as well, but even after I refactor those scripts I’ll still end up with an AI paddle script and a human player paddle script.
Component shows in the inspector, but now it appears I cannot drag a script onto a variable via the inspector.
Would it help if I showed all the code I have and listed all my gameobjects (and described what my game actually does, as this might be an xy problem)?
Maybe I’m understanding this wrong but couldn’t you make a base class which has the common interface, and then let the similar scripts inherit from this? The scipts that need to reference one of the similar scripts then gets a public var of the base class.
BaseClass.js
var goalCounter:int;
var specialMeter:int;
//other common vars and functions
PaddleRightBehaviorScript.js (and similar)
class PaddleRightBehaviorScript extends BaseClass
{
//whatever goes here
}
the updater script
#pragma strict
var scriptExtendingBaseClass:BaseClass; //drag your PaddleRightBehaviorScript (or similar) to this in Inspector
function OnTriggerExit(collider : Collider)
{
if (collider.tag == "Respawn")
{
if (scriptExtendingBaseClass.goalCounter > 0)
{
--scriptExtendingBaseClas.goalCounter;
}
if (scriptExtendingBaseClass.specialMeter > 0)
{
--scriptExtendingBaseClass.specialMeter;
}
}
}
function Update()
{
if (scriptExtendingBaseClass.goalCounter == 0)
{
Destroy(gameObject);
}
}
Theoretically that should work, but I still can’t drag scripts to the inspector. It only seems to work for other assets.
are you dragging the actual scripts from the project window?
You need to apply the components on GameObjects in your scene, THEN you can drag the applied component into the inspector.
This should also work:
If a GameObject A has a component that takes a var of type Class X and a GameObject B that has a component of Class X, you can drag GameObject B onto the var in the component in GameObject A. The var should automatically recognize the component from GameObject B.
Hope I didn’t make it sound more complicated than it is hehe…
Ah, alright. That makes sense. I’ll try that a little later. Thanks.