How do i get my player's coordinates?

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

“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.

2 Likes
  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

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class spawnFish : MonoBehaviour
{
    public GameObject fishModel;
       public int xPos;
    public int zPos;
    public int yPos;
       public int fishCount;
    public Transform player;

    void Start()
    {
        StartCoroutine(SpawnFish());
    }

    IEnumerator SpawnFish()
    {
        while (fishCount < 1000)
        {
            xPos = player.transform.x + Random.Range(10, 60);
            yPos = player.transform.y + Random.Range(10, 60);
            zPos = player.transform.y + Random.Range(10, 60);
            Instantiate(fishModel, new Vector3(xPos, yPos, zPos), Quaternion.identity);
            yield return new WaitForSeconds(0.1f);
            fishCount += 1;
        }
    }
}
  1. i want the fishModel to spawn near player (thats why i need player’s coordinates)

You’re missing a “.position” in there. transform.position.x

3 Likes

Do you feel mighty now?

He literally asked the OP to provide the information that would be needed to fix OP’s issue.

3 Likes

In line 12 you declare player, but it doesn’t seem to get initialized?

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?

2 Likes