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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

2D Help with Player prefab error

Discussion in '2D' started by _jacks, Oct 10, 2015.

  1. _jacks

    _jacks

    Joined:
    Nov 27, 2014
    Posts:
    27
    Hi all,

    Thanks for reading. I need some help with my game. I've got a character that transforms into another character and it uses player prefabs, the issue I'm having is when I initiate the transformation the old player gameobject is destroyed and a new player transform is instantiated.

    The issue is that there are enemy gameobjects which track the player and check its distance etc with every frame, I figured I would simply update the GameObject in the local enemies scripts but for some reason it doesn't work. The transform remains null after switching.

    I've also tried using a game manager which tracks the player using FindGameObjectWithTag and just referencing it via a static GameObject

    All of these produce the same result and I'm wondering if there is a simple solution that I have over looked

    So imagine I have an enemy game object which tracks the players distance if the player transform is null, just return. otherwise follow the player.

    Now the player transforms into another player. The enemy should reference the game managers reference to the player via a GameObject that is set as static. Which is updated after the player is destroyed and re-instantiated but for some reason the enemy remains stationary with a null player transform reference..

    Thanks
     
    Last edited: Oct 10, 2015
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    In the script where the enemies are identifiying the player can you put a check I. & if it returns null then do the search for the game object again?
     
  3. _jacks

    _jacks

    Joined:
    Nov 27, 2014
    Posts:
    27
    Thanks it seems to have worked even though I did it before and it didn't. So not sure why it works now.