- Home /
how to find the player without attaching a script to it
Hey. I want to start modding games but I quickly found out that I need to learn how to control everything without the editor, so I wanted to test some things and one of the things I wanted to do is to attach a movement script to the player via script but the script I wrote isnt working. here it is:
public float speed;
public GameObject Player;
void Start()
{
Player = GameObject.FindWithTag("Player"); //trying to find the player via tags
}
void Update()
{
//movement
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
Player.transform.position = movement * Time.deltaTime * speed;
//printing the player current position to the console
Debug.Log(Player.transform.position.x);
Debug.Log(Player.transform.position.z);
Debug.Log(Player.transform.position.y);
}
}
any ideas?
First - I think you probably meant
Player.transform.position += ...
As far as finding the player - are you getting null reference exceptions to show that you did not find the player object? If it is getting to those Debug.Logs then you did find an object tagged as player.
Do you know that the Player object is Tagged as a player? If not, what do you know about the player object? If you know its name, you can use GameObject.Find(), if you know any script that is attached to it you can use GameObject.FindObjectOfType. Do you know if the player is publicly referenced by any other scripts, like a Game Manager script perhaps? If so you can grab the reference from that script. Other than that, if you don't know anything about the player object it will be pretty difficult to find it in the sea of objects in the scene.
You are not finding player by script you are finding it by tag, you assign tags in the editor. If you put a script on it you can find it like that as well, you can also set the reference in the inspector.
Answer by UnityPlum · Nov 01, 2021 at 01:04 AM
Did you add the Tag "Player" to the player object? If you did, try, GameObject.Find("Player")
and name the Player object to "Player"
hey yeah I added the Tag but the script isn't even working like it doesn't even debug the player position until I attach the script to an object in the editor but I was hoping I could get it to work without attaching the script to something cuz In modding you cant really do it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613044746im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Character motor movement and jump 1 Answer
add force in a particular direction 0 Answers
List of C# scripting definitions 1 Answer
Some basics 1 Answer
How Unity provide ability to play Android, iOS configurations on PC/macOS editor? 0 Answers