Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How Can I Set who Launched the Misile in my game

Discussion in 'Scripting' started by Mondole, Nov 17, 2016.

  1. Mondole

    Mondole

    Joined:
    Nov 16, 2016
    Posts:
    6
    I have many characters and many Misile kinds so I want to access from the misile script to the Character who instantiated it to increment its score if it hits the enemy. I tried used One but it changes all the Misiles in the Scene to the last one that launched the Misile.

    this is the Script of the one to Launch the misile, but it change the Fields of all the misiles in the scene even if they was not instantiated by It
    Code (CSharp):
    1. public class LAUNCH : MonoBehaviour {
    2.     public bool CounterInit = true;
    3.     public float CD;
    4.     public GameObject MisileA;
    5.     public Transform LaunchPoint;
    6.     int Rotation = 0;
    7.  
    8.     void Update ()
    9. {
    10.         if (Input.GetKeyDown("space") && (CD == 0.00f))
    11.         {
    12.             Instantiate(MisileA, LaunchPoint.position, Quaternion.Euler(0, 0, Rotation));
    13.             CD = 15.0f;
    14.             MisileA.GetComponent<MisileA>().MisileOwner = this.gameObject;
    15.         }
    16. }
    and the code of the misile is

    Code (CSharp):
    1. public class MisileA: MonoBehaviour{
    2.     Vector2 PositionIn;
    3.     float Diverg = 30.00f;
    4.     float XActual;
    5.     float YActual;
    6.     public GameObject MisileOwner //Who ever Launces this Misile
    7.  
    8.     void OnCollisionEnter2D(Collision2D coll)
    9.     {
    10.         if (coll.gameObject.tag == "ENEMY")
    11.         {
    12.             coll.gameObject.GetComponent<INIT>().Anger + = 1;
    13.             coll.gameObject.GetComponent<INIT>().Health+ = 1;
    14.             coll.gameObject.GetComponent<INIT>().Score  - = 20; //The one who is HIT
    15.             MisileOwner.GetComponent<INIT>().Score += 100; //The one who LAUNCHES
    16.         }
    17.     }
     
  2. absolute_disgrace

    absolute_disgrace

    Joined:
    Aug 28, 2016
    Posts:
    253
    ** Please note in the below I've used the correct spelling of missile for my variables but maintained your variable names as is. **

    Add a function to your Missile script along the lines of:
    Code (CSharp):
    1. void ownedBy(gameObject newMissileOwner){
    2. MisileOwner = newMissleOwner;
    3. }
    Then change line 14 in your update to:
    Code (CSharp):
    1. MisileA.GetComponent<MisileA>().ownedBy(playerGameObject);

    playerGameObject is going to be the game object of the player who is able to press space (which you should know somewhere in your game). this.gameObject isn't going to work for you here i don't believe.
     
  3. Mondole

    Mondole

    Joined:
    Nov 16, 2016
    Posts:
    6
    Thank you for Answering and I'm Sorry for the orthography. That works but I don't know how to trace who was the one that Launched de Missile. So I want to storage somehow the owner of the Missile in the Missile script at the moment it is instantiated.
     
  4. absolute_disgrace

    absolute_disgrace

    Joined:
    Aug 28, 2016
    Posts:
    253
    In the provided code you are using the pressing of space to be the person launching the missile. That makes me believe that the only person who could launch the missile in the provided code is the player.

    The other occasions (computer controlled players?) i would imagine you would have another method that spawns the missile and at that stage you would have the related game object. You would call the same line and instead of using the "playerGameObject" you would just use whichever one is launching that missile.
     
  5. Mondole

    Mondole

    Joined:
    Nov 16, 2016
    Posts:
    6
    I need to make it this way because Computer controlled players can turn into Playable characters keeping its score and register, I will only change the way it gets the input when the Character is not cotrolled by the Player.
     
  6. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    You're setting the value on LAUNCH.MisileA, which is the prefab you're instantiating from, but is not the new missile that you created. You'd want to do:
    Code (csharp):
    1. var newMissile = Instantiate(MisileA, LaunchPoint.position, Quaternion.Euler(0, 0, Rotation)) as GameObject;
    2. newMissile.GetComponent<MisileA>().MisileOwner = this.gameObject;
    Also, calling the variable in LAUNCH the same name as the class (MisileA) is confusing, you should change it.