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

How do i get my player's coordinates?

Discussion in 'Scripting' started by owlthesheep3, Oct 30, 2019.

Thread Status:
Not open for further replies.
  1. owlthesheep3

    owlthesheep3

    Joined:
    May 13, 2019
    Posts:
    35
    I need to check player's x,y,z coordinates via C# Script... i tried int
    ypos =player.transform.y
    but it doesnt work! can anyone get me the right piece of script? thx
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    "it doesn't work" is not helpful.

    Instead, try to be a little more descriptive:

    1. post the actual error, full text copied from the log

    2. post your actual code. See the first post in this forum for code formatting help.

    3. explain what you want, versus what is happening.
     
  3. owlthesheep3

    owlthesheep3

    Joined:
    May 13, 2019
    Posts:
    35
    1. Error CS1061 'Transform' does not contain a definition for 'x' and no accessible extension method 'x' accepting a first argument of type 'Transform' could be found (are you missing a using directive or an assembly reference?) Assembly-CSharp 23 Active
    Error CS1061 'Transform' does not contain a definition for 'y' and no accessible extension method 'y' accepting a first argument of type 'Transform' could be found (are you missing a using directive or an assembly reference?) Assembly-CSharp 24 Active
    Error CS1061 'Transform' does not contain a definition for 'z' and no accessible extension method 'z' accepting a first argument of type 'Transform' could be found (are you missing a using directive or an assembly reference?) Assembly-CSharp 25 Active

    2.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class spawnFish : MonoBehaviour
    6. {
    7.     public GameObject fishModel;
    8.        public int xPos;
    9.     public int zPos;
    10.     public int yPos;
    11.        public int fishCount;
    12.     public Transform player;
    13.  
    14.     void Start()
    15.     {
    16.         StartCoroutine(SpawnFish());
    17.     }
    18.  
    19.     IEnumerator SpawnFish()
    20.     {
    21.         while (fishCount < 1000)
    22.         {
    23.             xPos = player.transform.x + Random.Range(10, 60);
    24.             yPos = player.transform.y + Random.Range(10, 60);
    25.             zPos = player.transform.y + Random.Range(10, 60);
    26.             Instantiate(fishModel, new Vector3(xPos, yPos, zPos), Quaternion.identity);
    27.             yield return new WaitForSeconds(0.1f);
    28.             fishCount += 1;
    29.         }
    30.     }
    31. }
    3. i want the fishModel to spawn near player (thats why i need player's coordinates)
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    You're missing a ".position" in there. transform.position.x
     
    Bunny83, JeffDUnity3D and Kurt-Dekker like this.
  5. AlexiFullerman

    AlexiFullerman

    Joined:
    May 1, 2020
    Posts:
    6
    Do you feel mighty now?
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    He literally asked the OP to provide the information that would be needed to fix OP's issue.
     
  7. Steen23

    Steen23

    Joined:
    Aug 6, 2023
    Posts:
    1
    In line 12 you declare player, but it doesn't seem to get initialized?
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Please don't necro post to old threads. Start your own post if you have an issue. It's FREE!

    Look up Unity's in-built serialization. It's what EVERYTHING is based on in Unity. It's just dependency injection and there are very well-documented rules scattered all over the web and learning materials.

    If you have an actual problem, make your own post. Here is...

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven't put effort into finding the documentation, why should we bother putting effort into replying?
     
    Bunny83 likes this.
Thread Status:
Not open for further replies.