- Home /
A way to set object's script as a variable of sorts?
I have some background in C# programming, but am a newbie of sorts with Unity
I'll get to the main point, I'm currently working on a mecha game where you can swap out weapon parts on the player character, I've got everything nailed down, except that swapping weapons part.
All of the weapons are objects with scripts, instantiated and parented to the PC's weapon point on game start, but I'm kinda lost as I need to fire the weapons and different weapons have different script.
So, is there anyway that I can grab the object's script and use it like a variable?
Not sure if I'm painting a clear picture but here's the code sample :
public class PlayerActionscript : MonoBehaviour
{
PlayerRocketLauncher mWeapon1;
public GameObject mWeapon_R_U;
// Use this for initialization
void Start ()
{
mWeapon1 = mWeapon_R_L.GetComponent<PlayerRocketLauncher>();
}
// Update is called once per frame
void Update ()
{
if(mIsFiring){mWeapon1.mIsFiring = true;}
}
Another way that I think would work is that every time the script updates I'll make a check to see what sort of weapon it is then activate the shooting script, something like this :
public GameObject mWeapon_R_U;
if(mWeapon_R_U.name.Contains("RocketLauncher"))
{
mWeapon_R_U.GetComponent<PlayerRocketLauncher>().mIsFiring = true;
}
So, yeah, is there some way to grab an object's script and use it like a variable or I'm stuck with using the messy method?
Answer by cagezero · Dec 19, 2012 at 07:52 PM
Sounds like you need to use either an abstract class or an interface for the weapon variable. Each weapon (like your Rocket Launcher) should then either extend or implement that abstract class or interface and the PlayerActionscript won't have to worry about what type of weapon it is referencing, it can just fire it.
Here is an example of the abstract class way:
public class PlayerActionscript : MonoBehaviour {
//Might be a RocketLauncher, might be a Pistol. PlayerActionscript does not care.
Weapon mWeapon1;
// Use this for initialization
void Start ()
{
mWeapon1 = GetComponent<Weapon>();
}
// Update is called once per frame
void Update ()
{
if(mWeapon1.IsFiring()){mWeapon1.SetIsFiring(true);}
}
}
public abstract class Weapon : MonoBehaviour {
//A variable visible to all subclasses
protected bool isFiring;
//Functions with the same implementation for all Weapons
public bool IsFiring() {
return isFiring;
}
public void SetIsFiring(bool firing) {
isFiring = firing;
}
//Functions with the same implementation for most weapons
public virtual void Reload() {
}
//Functions requiring custom implementations by all Weapons
public abstract void Fire();
}
public class RocketLauncher : Weapon {
//A variable only visible to the rocketlauncher
GameObject rocket;
//Reloading a RocketLauncher probably takes a little longer. Override to customize
public override void Reload() {
}
//All weapons have to provide cutom firing code
public override void Fire() {
}
//Put some private RocketLauncher functions here.
}
[removed vague weapon switching comment. not sure why it was there in the first place @_@]
Hope that helps.
Wow, that's something new for me. I think I get what you mean, but I'm still kind of confused, because my rocket launcher actually have a script that does its own thing, but from what I see here is that I need to move the rocket launcher script into another class in PlayerActionScript?
edited my answer for clarity and removed the comment about weapon switching.
Without seeing the rest of your code and project setup I can only speak to best practices. In general, you will want as little dependency between the PlayerActionscript and the actions it controls. So, no. I do not think moving the RocketLauncher script into the PlayerActionscript is necessary. For every action you need a Weapon to have, add a function to the Weapon class and have the subclass implement it.
I am not sure what your level of comfort is with different oop paradigms, but it seemed like these links might be useful.
http://answers.unity3d.com/questions/233859/whats-the-use-of-abstract-classes-and-interfaces-i.html
http://msdn.microsoft.com/en-us/library/scsyfw1d%28v=vs.71%29.aspx
http://stackoverflow.com/questions/56867/interface-vs-base-class
aye, that did clear up lots of stuff. thanks for the help.
Your answer
Follow this Question
Related Questions
Access object from script problem. 1 Answer
Same script not working on all objects 1 Answer
Object doesn't stick on Camera right Corner?? (Basic Question) 1 Answer
Get GameObject name from other object. 2 Answers
Drag Script Not Working 1 Answer