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

Question Unity2D - Enemies prefabs don't damage player

Discussion in 'Scripting' started by Lok_7, Aug 16, 2023.

  1. Lok_7

    Lok_7

    Joined:
    Aug 11, 2023
    Posts:
    3
    i'm doing a project where the player defeats enemies that appear infinitely. but when i create prefabs of an enemy, the prefabs can't read the references to the player's GameObjects, i managed to make the prefabs follow the player by calling by GameObject in void start with "void Start(){ playerPosition = GameObject.FindGameObjectWithTag("Player").transform; }"

    but I couldn't reproduce that so the enemy can damage the player "void Start(){ playerBlood = GameObject.FindGameObjectWithTag("Player"); }"

    I'm stuck on what to add after the "FindGameObjectWithTag("Player")"
     
  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    I would just make the player script have a static instance, so each new enemy can easily just call:
    Code (CSharp):
    1. Vector3 playerPos = Player.instance.transform.position;
    2. MoveTowards(playerPos);
    3. Player.instance.TakeDamage(15f);
    Since you have many enemies, and just one player, enemies calling a static reference to player would be the best way to go.

    Also would research into object pooling(make your own method). But pooling is just disabling the enemies object(instead of Destroying) and setting them back to active(instead of Instantiating). Saves a lot on performance, if using a bunch of enemies, which ultimately means you can have more enemies on screen with same frame count.
     
  3. Lok_7

    Lok_7

    Joined:
    Aug 11, 2023
    Posts:
    3
    hey, thanks for the suggestion!
    I've read a bit about instantiations and it can really be useful, but how would I specifically instantiate the player's position and TakeDamage functions?
    Sorry if it's a stupid question, should I just create a prefab of the player as well? so I can call it in the scripts of the enemy prefabs
     
  4. Lok_7

    Lok_7

    Joined:
    Aug 11, 2023
    Posts:
    3
    Don't worry, I managed to fix everything. Thank you!
     
  5. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    By basically making a static "script", most people refer to it as "instance". Then in the player Awake() method just say
    staticPlayerScriptName = this;
    . You can have it either in player itself, or another class if you like to group certain things, just gotta call the class then dot instance
    OtherScript.playerInstance.DoStuff();
    . But you'll mostly see it written out in other posts as
    Player.player.DoStuff();
    , as you can name the instance anything you want.

    And once you have a static reference to a script, you can easily call any public variable, or public function. :)