How do i change a variable from one Class to another Class?
I have a Player class (overly simplified here), with a variable which holds a Ship class (also overly simplified):
// all the normal includes and usings
public class player : monobehaviour
{
Ship ship_;
public void Start()
{
// The below methods go here
ship_.Start();
shipName_ = ship_.name_;
}
public void ShootSmall()
{
// Set the position to instantiate at
Vector3 Pos = transform.FindChild("Barrel").position;
// create the new bullet
Transform bulletToFire = ship_.bullet_;
//set default variables - Owner of bullet, direction to fire, bullet type
bulletToFire.GetComponent<AbsBullet>().SetUp(this, dir_, ship_.bulletType_);
((Transform)Instantiate(bulletToFire, Pos, Quaternion.identity)).LookAt(transform.position + (dir_ * 100));
}
}
public class Ship : monobehaviour
{
public string name_ = "myshipsname";
public Transform bullet_;
public ProjTypes bulletType_ = ProjTypes.StdSlug;
public void ShipDestryed()
{
// destroy the gameobject
Destroy(gameObject);
//Resources.UnloadAsset(this);
}
}
i want to allocate and swap the ships at runtime. i would have expected to use the new keyword here but of course i cannot.
I have tried the following before anything else in the start function:
ship_ = ((GameObject)Resources.Load("DerrivedShipPrefabName")).GetComponent<Ship>();
// the above is a prefab in the resourcesfolder with the script attached
ship_ = gameObject.AddComponent<DerrivedShip>() as Ship;
// as a pair
gameObject.AddComponent<DerrivedShip>();
ship_ = gameObject.GetComponent<DerrivedShip>();
the ship and all of its derrived calsses have their own files.
My question is this how do i correctly change between derrived ships?
A secondary question which has lead me to this one is that if i use the resources method all seems well but i cannot destroy the gameobject that the player (and therefore the derrived ship) is attached to. Perhaps my problem lies there and not in the above. In which case how do i go about destroying such an object, as Destroy(gameObject) gives me the error about not being able to delete assets.