Domanda

My simple script is supposed to follow the player, but it stopped finding the game object with the "Player" tag... The only solution I could find was to make a new tag, assign it to my player, then change the tag names in all the scripts.

I found another way to find the player object, and I like it better, but I'm new to this and have no idea if it will affect performance. Here was my original code that couldn't correctly find the tag

public Vector3 offset;
private Transform player;

void Awake (){
    player = GameObject.FindGameObjectWithTag("Player").transform;
}
void Update (){
    transform.position = player.position + offset;
}    

If I make it public so I can drag my player object onto the script in the inspector, is that going to affect performance and is it not a good idea?

My common sense is telling me that is a bad way to do it because I've seen other peoples scripts and they use the FindGameObjectWithTag(). Why go through the trouble of writing the extra lines when you could just do it like this...

public Vector3 offset;          // The offset at which the Health Bar follows the player.   
public Transform player;        // Reference to the player.

void Update ()
{
    transform.position = player.position + offset;
}

Someone please let me know because I'm new to this and I don't want to start off doing things the wrong way. Thanks!

È stato utile?

Soluzione

Your second code is absolutely right. Some people use FindGameObjectWithTag() but others do use a public variable. That's the whole reason for Unity to offer them in the editor, to start with. So you can just drag stuff to scripts :)

I personally use the public variable when I'm in a situation that is just like yours, that is, you have one script and one Player that you want it to be attached to the script.

I consider that in this case, using a public variable is actually the right thing to do. Imagine if you decide later to make another Player prefab, switch to that, do some tests, switch back later... you could easily change them in the editor. If you used FindGameObjectWithTag(), you'd have to change tags on both prefabs. Error prone.

Now imagine if you have multiple scripts using the Player. In this case, if you want to change the Player prefab, you'd have to change manually all the references in the editor. That's a lot of work, so maybe you'd just want to re-tag stuff.

In terms of performance, using the public variable is actually faster because it's a direct reference to the object, while FindGameObjectWithTag() would look for the object. But this doesn't matter really, because FindGameObjectWithTag() is very fast.

Altri suggerimenti

It shouldn't be a problem of performance. FindGameObjectWithTag returns a list of Objects matching the tag you pass as parameter, it's generally used when there are several objects having that tag. In a scenario in which you have many objects with a tag, and you need a reference to them all, referencing them by hand through public variables isn't practical; even more, there might be objects dinamically generated that you can't reference before they're created.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top