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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Transform.position not changing

Discussion in 'Scripting' started by lucadrefke, Apr 19, 2018.

  1. lucadrefke

    lucadrefke

    Joined:
    Apr 15, 2018
    Posts:
    43
    Hello!
    I want to read in the position of my player. It is a prefab and will be spawned then starting the Game at the position (0|0|0). Now, in the script of the EnemyKI (AI) I want to read in the position of the player. The problem is, that the position is not updating then reposition the player.
    Hope you can help me!
    Here my script:

    Code (C#):
    1.  
    2. using System.Collections;
    3. using UnityEngine;
    4.  
    5. public class EnemyAI : MonoBehaviour  {
    6.     public GameObject target;
    7.     public float moveSpeed;
    8.     public float rotationSpeed;
    9.  
    10.  
    11.     void Start() {
    12.         target.transform.position = new Vector3 (0, 0, 0);
    13.  
    14.         Debug.Log (target.transform.position);
    15.  
    16.  
    17.     }
    18.  
    19.  
    20.  
    21.  
    22.     void Update() {
    23.         Debug.DrawLine (transform.position, target.transform.position);
    24.  
    25.         Vector3 pos = target.transform.position;
    26.         Debug.Log (pos);
    27.     }
    28.  
    29.  
    30. }
    31.  
    I dragged the player prefab into the field in the inspector.
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,383
    What are you expecting to happen?
     
  3. lucadrefke

    lucadrefke

    Joined:
    Apr 15, 2018
    Posts:
    43
    I want the position witch is wirtten on the console to update then I change player position. Later I want to use the player position to let the enemy chase the player
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,383
    Currently you're forcing
    target
    to 0,0,0 and logging that vector to the console. That's pretty much it. It's repeating 0,0,0 in the console because you've told it to do exactly that.

    If you expect something else to happen, you should probably write some more code to facilitate that.
     
  5. lucadrefke

    lucadrefke

    Joined:
    Apr 15, 2018
    Posts:
    43
    But In the Update loop I rewrite the position to the console. So why the position is ever (0|0|0)? I am moving the Player!!!
    Code (C#[ using System.Collections; using UnityEngine; public class EnemyAI : MonoBehaviour { public GameObject target; public float moveSpeed; public float rotationSpeed; void Update() { Debug.DrawLine (transform.position, target.transform.position); Vector3 pos = target.transform.position; Debug.Log (pos); } } [/Code):
    1.  
     
  6. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,383
    Because you are pointed at the Prefab, not the Scene Object.
     
  7. lucadrefke

    lucadrefke

    Joined:
    Apr 15, 2018
    Posts:
    43
    And how to get the Object in the game?
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    One easy way of doing that is reference the scene player in your enemy spawning script.
    When any enemy is spawned, pass the reference to the newly instantiated enemy.
    :)
     
  9. lucadrefke

    lucadrefke

    Joined:
    Apr 15, 2018
    Posts:
    43
    Can you show me that in code please?
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You should really consider doing some beginner tutorials, because I explained it simply.
    It would look something like this.
    Code (csharp):
    1. public GameObject enemyPrefab;
    2. public GameObject target; // player
    3.  
    4. void SpawnEnemy() {
    5.    GameObject go = Instantiate(enemyPrefab);
    6.    go.GetComponent<EnemyAI>().target = target; // assign the reference from here to there.
    7.  }
    Something like that.
     
  11. lucadrefke

    lucadrefke

    Joined:
    Apr 15, 2018
    Posts:
    43
    But the reference to the player is the same. I am drag and drop the prefab to the script. What is the difference between yours and mine? Or is it only working from Objects witch are on the Herachy?
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    No, no.. I assume the game object that spawns the enemies is in the scene when you begin. I also assume that the player, too, is in the scene. You would drag the player from the hierarchy to this variable.
     
  13. lucadrefke

    lucadrefke

    Joined:
    Apr 15, 2018
    Posts:
    43
    No. My player is a prefab and will be instantiated by a player spawning script
     
  14. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, is the player spawning script/object in the scene when it starts, along with the enemy script that spawns? :)
     
  15. lucadrefke

    lucadrefke

    Joined:
    Apr 15, 2018
    Posts:
    43
    I've got a GameObject in my Scene witch hold a script for enemy, and player spawning.
     
  16. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay then, so to elaborate on my earlier example with a small modification:
    the player spawning script has a reference to the enemy script.
    the player spawning script spawns the player and retains the reference.
    - script passes the newly made reference to the enemy spawner

    the enemy spawner instantiates enemies and passes the reference of the player in the scene along to them.

    This is just one way. Does it make sense now?
     
  17. lucadrefke

    lucadrefke

    Joined:
    Apr 15, 2018
    Posts:
    43
    But how to get the reference of the player? Its a prefab.
     
  18. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    So, when it spawns the player from the prefab, it stores the instantiated copy in a variable. Uses that to pass along...
     
  19. lucadrefke

    lucadrefke

    Joined:
    Apr 15, 2018
    Posts:
    43
    Ahhh And I pass this Object to the Enemy spawner script and this passes it to the enemyAI script?
     
  20. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Exactly...
     
  21. lucadrefke

    lucadrefke

    Joined:
    Apr 15, 2018
    Posts:
    43
    But before I wanted to rewrite the AI, its works with GameObject.FindGameObjectWithTag("Player"). But now it doesn't work anymore.
    But thank you for your help
     
  22. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sure, tag would work. I couldn't understand from your latest reply what doesn't work anymore, but hopefully you can figure out which one to use.
    I prefer to avoid using 'Find', whether by tag or name, but the option is always there. :)
     
  23. lucadrefke

    lucadrefke

    Joined:
    Apr 15, 2018
    Posts:
    43
    Okay Thank you very much.
    But what to do then I've got more then one player? (In my case a fleat of spaceships)
     
  24. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, then you'd have a list of players the enemy might be interested -- limited perhaps by threat* or proximity or some other value/variable. If that's what you meant..

    edit: fixed typo.
     
    Last edited: Apr 20, 2018
  25. lucadrefke

    lucadrefke

    Joined:
    Apr 15, 2018
    Posts:
    43
    Hmmm I don't know. But thank you for your help.
     
  26. unity_BSj7OVy3Bqg8rw

    unity_BSj7OVy3Bqg8rw

    Joined:
    Nov 4, 2018
    Posts:
    1
    So what exactly is the solution ? How can I update the transform of a gameObject that is instantiated from a prefab and print out every single point when im moving it ? I am trying to use bounds.contains but it does not update the position of the prefab, it just stucks at the instantiation position even when its in Update(). How can I update and print the exact position of a gameObject which is instantiated whenever it moves?
     
  27. shariarp

    shariarp

    Joined:
    Aug 9, 2019
    Posts:
    2
    Solution: the only solution I found is to duplicate which over object your trying to apply the transform to and delete the old one.

    explanation: its not a issue cause you're doing something wrong.. its some internal issue which causes the transform of that specific object to be immune to any changes through scripts. I wasted hours on it before realizing there is nothing wrong with my code its just Unity had a screw lose somewhere internally causing this problem,
     
    GleaveGames likes this.
  28. maclo4

    maclo4

    Joined:
    Apr 8, 2021
    Posts:
    1
    FOR ANYONE WHO IS STRUGGLING WITH THEIR GAME OBJECT SUDDENLY NOT BEING MOVEABLE:

    I couldnt find this almost anywhere online, but for me this issue was caused by the animator. Try disabling the animator and see if you can move now. If you can, then there is at least one animation in which the game object as a whole (not individual bones) is moved/translated. Delete or change the animations and it should let you move now
     
    FairdaElOraby and Kurt-Dekker like this.
  29. vitorwg96

    vitorwg96

    Joined:
    Jul 26, 2021
    Posts:
    1
    I think u can't instantiate the objectPrefab on Inspector if u want get the current position each time.

    So, i used FindObjectOfType<T> the Type is the name of the script attached the Player :
    Code (CSharp):
    1. private Transform _targetPos;
    2.  
    3. private void Awake()
    4. {
    5.    _targetPos = FindObjectOfType<PlayerController>().GetComponent<Transform>();
    6. }