Search Unity

Question Rocket prefab dont follow an existing GameObject in the Game Scene

Discussion in '2D' started by PlyfexHD, Jul 30, 2020.

  1. PlyfexHD

    PlyfexHD

    Joined:
    Jul 5, 2020
    Posts:
    4
    Hello, first I need to explain how my game works.

    You are a Player (White circle) and a rocket spawn and follow a public target till it hits the target(the player) and destroy itself with help from a script.(All normal without any prefabs)

    Then I created a GameObject with the name Spawner and the Spawner has a script to spawn a prefab every 3 seconds with help of youtube.

    Then I make a prefab from my rocket and delete the 1 rocket from the Hierarchy. Then I add this rocket prefab to my Spawner.

    My problem now is that

    The rocket prefab doesn't have any target because I can't drag the Player from the Hierarchy in the target field. I could make a prefab from my Player and then add it to the target field but then my rocket doesn't follow the player prefab in the game.

    How could I make it that the rocket prefab that always spawn follow a non prefab GameObject(My Player)?

    I hope that I described it well and everyone can understand my problem.
     
  2. QuentinBacuet

    QuentinBacuet

    Joined:
    Nov 1, 2017
    Posts:
    13
    You could pass your player to the Spawner GameObject (the same way you did previously for the rocket). You can add some sort of setter (or with properties in C#) in the Rocket class, that will set the player (your target):

    private void SetTarget(Player player){
    this.player = player;
    }


    When you spawn a Prefab (in C#):

    var o = Instantiate(rocketPrefab, Vector3.zero, Quaternion.identity);
    Rocket r = o.GetComponent<Rocket>();
    r.SetPlayer(player);


    I hope I understood your problem.
     
  3. PlyfexHD

    PlyfexHD

    Joined:
    Jul 5, 2020
    Posts:
    4
    Man I tried and tried but I have not much experience to do something in this S***, I will give up thx for helping.

    I dont get the logic from C#