- Home /
Possibility of using similar name variables from different scripts.
Hello world.
I'm trying to implement the input script, both for the player and the AI for a racing game. And in order to optimize the code, I use the same variables in either script.
public class PlayerScript : MonoBehaviour{
public float accel;
public float brake;
public float steer;
public bool boost;
//stuff
}
public class MachineScript : MonoBehaviour{
public float accel;
public float brake;
public float steer;
public bool boost;
//stuff
}
And I call them in the script that controls the car movement.
public class CarMovement : MonoBehaviour
{
//variables
PlayerScript pInp;
MachineScript mInp;
void Start () {
if (gameObject.GetComponent("PlayerScript") as PlayerScript != null)
{
pInp = GetComponent<PlayerScript>();
}
else if(gameObject.GetComponent("MachineScript") as MachineScript != null)
{
mInp = GetComponent<MachineScript>();
}
}
//stuff
}
At the moment, this script is working with the player script only (pInp). But eventually we will start working on the AI,and the idea is that the movement script has no major alteration.
Is it possible to create a variable to use according to the script? And if so, how?
Thanks in advance.
Answer by Vega4Life · Jul 31, 2019 at 04:03 PM
Can't we just do some inheritance here? Quick example:
public class MovementScript : MonoBehaviour
{
protected float accel;
protected float brake;
protected float steer;
protected bool boost;
//stuff
}
public class PlayerScript : MovementScript
{
// We have access to all the things for movement
//stuff
}
public class MachineScript : MovementScript
{
// We have access to all the things for movement
//stuff
}
So now the machine or player can share their movement - since you are saying its the same basically. Then it makes it much easier to manage it later.
public class CarMovement : MonoBehaviour
{
//variables
MovementScript movementInp;
void Start()
{
movementInp = GetComponent<MovementScript>();
// Are we a player ... a machine... really doesn't matter
// Unless we doing something specific to the player or machine
if (movementInp is PlayerScript)
{
// do specific thing
}
else if (movementInp is MachineScript)
{
// do specific thing
}
}
//stuff
}
Now we just make the script do whatever because they are the same, UNLESS you want to do something totally specific to the player or machine. Maybe this helpful.
Beat me to it! Good job for not only beating me to the answer, but providing a more complete solution as well
Answer by I_Am_Err00r · Jul 31, 2019 at 04:04 PM
Try inheritance instead of what you are doing.
You can have all the shared variables in the parent class, then make the player and AI inherit from the parent, which will be a much better approach to handle the issue you are facing.
Your answer

Follow this Question
Related Questions
How Do I Access and Change Items in a List on Another Script? 2 Answers
Using a variable value with GetComponent 1 Answer
Random selection 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers