Hi, so I’ve got a situation where I have several object that each have a unique script on them. The scripts can’t be combined because they are different in function. Also on each object are other scripts that are the same, but they have to access the unique scripts. Because of this, I have to assign that unique script at runtime, like this:
So far, that works fine, and I hope it makes sense. The problem is, now in the other scripts that need to access the variables of that unique script, I get the following error:
error CS1061: Type UnityEngine.Component' does not contain a definition for power’ and no extension method power' of type UnityEngine.Component’ could be found. Are you missing an assembly reference?
This is still in the editor. I can’t even get to the point of runtime because of course I can’t assign the scripts until all the errors are cleared.
So I guess the question is: if I’m assigning one of several scripts to an object at runtime, how do I write the other scripts so they can access variables on that script (after it’s assigned) but not cause errors in the editor because it doesn’t find that script yet?
Well, first of all it looks like your uniqueScriptName will never be the component name which means the GetComponent will always fail.
Second, what is this component actually supposed to do? If you have these other components already on the object then why cant you just GetComponent them instead of using this proxy script?
I have a whole bunch of different vehicles. Each one contains a script that handles the ultimate movement, etc, so that one is the same across all of them. Each vehicle handles differently: not just changes to stats, but in how things are calculated. This is why there is a different script on each vehicle. The generic movement script then accesses the resulting output of the individual vehicles’ calculation script. This is why the calculation script (the unique one) has to be assigned at runtime. Otherwise I would have to also have a unique version of each movement script, which will very quickly get very untidy.
To clarify a bit, I’ve got one series of scripts that we’ll call CalculationScript1, CalculationScript2, CalculationScript3, and so on. Each one assigned to a different vehicle.
I have one other script called MovementScript. It’s assigned to all vehicles.
In MovementScript I’d like to do the following:
public class MovementScript : MonoBehaviour
{
public Script calculationScript;
private float variable1;
void Update()
{
variable1 = calculationScript.variable;
calculationScript.DoThis();
}
}
But of course there is no such thing as a “Script” variable. So I’m trying to find a workaround for it.
And to back up a bit, I don’t actually need to assign this at runtime. That’s just what I’d read on different threads. Both scripts will already be on the vehicles, so I’m just searching for a way to assign the first one to the second one without having to rewrite that second script for each and every vehicle just to change that one line with the name of the first script.
Because each vehicle’s calculation script is substantially different and therefore requires a different name.
So “public ISpecialVehicle” isn’t possible. it would have to be “public ISpecialVehicle1”, “public ISpecialVehicle2”, “public ISpecialVehicle3”, and so on. Otherwise Unity throws an error because the name of the variable (ISpecialVehicle) doesn’t match the name of the class it’s trying to reference (public ISpecialVehicle1, public ISpecialVehicle2, public ISpecialVehicle3, etc).
Thanks for the help, but I believe at this point that there is no way to do this. I’m just going to have to have a completely unique set of scripts on each vehicle. It’s doable, but very messy. I was just searching for a way to tidy things up a bit.
If the unique scripts exist, then you can put an interface on them. If there is an interface, then you are able to communicate through the interface.
public Interface ISpecialVehicle // Here's your magical unique interface.
{
public int someVariable {get;set;}
public string someString {get;set;}
public void UniqueControlMath();
}
public class Vehicle57 : Monobehavior, ISpecialVehicle // Here's some random vehicle with unique stats
{
public int someVariable {get;set;}
public string someString {get;set;}
public void UniqueControlMath()
{
// Up equals down and multiple someVariable by 10,000!
}
}
Set those to whatever you want, they’re now totally unique to Vehicle 57.
public class Vehicle83 : Monobehavior, ISpecialVehicle
{
public int someVariable {get;set;}
public string someString {get;set;}
public void UniqueControlMath()
{
// Left is Up and multiple input by someVariable!
}
}
Set those to whatever you want, they’re now totally unique to Vehicle 83.
// this is the only controller you need for an unlimited number of super-special-unique vehicles. It is vehicle-agnostic.
public class MyTotallyGenericController : Monobehavior
{
public ISpecialVehicle thingToControl;
public void Start()
{
thingToControl = GetComponent<ISpecialVehicle>();
}
public void Update()
{
// doesn't even care what is in the method, just runs it.
thingToControl.UniqueControlMath(); // unique to whatever vehicle script its pointed at.
}
}