- Home /
How to assign a transform object to Prefab ?
Hello Developers, I've got a problem right here. I've got 2 spaceships and their AI script. I can't assign my game object to theese scripts. I think that's because of i used "Transform" method. But When i change it to "GameObject.FindWithTag("Player");" transform method and follow method colappses. So what should i do ?
enter code herevar moveSpeed : float;
var player : Transform;
var distanceToPlayer : float;
function Update()
{
transform.LookAt(player);
if ((Vector3.Distance(transform.position, player.position) > distanceToPlayer))
{
transform.position += transform.forward * moveSpeed * Time.deltaTime;
}
else
{
transform.rotation.y = 180;
transform.position += transform.forward * moveSpeed * Time.deltaTime;
}
}
my code is this.
Age 14, Turkey.
$$anonymous$$aybe you should use GameObject.FindWithTag("Player").transform
to get player's Transform component for your AI script?
Answer by BoydHamilton · Sep 19, 2020 at 07:06 PM
player = GameObject.FindWithTag("Player");
target = player.transform;
get the gameobject, then get the gameobjects transform. this worked for me, hope it works for you as well!
Answer by CHPedersen · Jan 29, 2014 at 12:04 PM
It doesn't have anything to do with your use of a public Transform variable to hold a reference to the player. Instead, it is probably because Unity doesn't serialize scripts (and public variables) until all scripts pass compilation. Serialization is the feature that makes stuff appear in the Inspector. I don't see anything wrong with your script as posted above, but perhaps there are still compiler errors elsewhere in your code?
Clear all compiler errors, and then try again. "Player" should appear in the Inspector in the editor for this script, and then you can drag the gameobject onto it.
this is what i mean; I can't able to attach my player when i try to attach, that kinda
screen appears and nothing else.
Ah, okay, that's because you're clicking the little dotted circle, which makes the editor look for assets to attach. But a "Transform" is not a file, and so does not appear as an Asset file in this list.
To attach it, drag and drop the Player gameobject with your mouse from the Hierarchy to the script ins$$anonymous$$d.
But i need it as Transform, Not GameObject, When i turn it to GameObject; I can't able to use transform operations (Like, position, rotation etc.) and it's not allowed, I can't attach :S
You don't have to change it to GameObject. Dragging a GameObject from Hierarchy onto a variable of type Transform will set that variable to Transform too. Just try it.
Oooh, right, then I get it. Sorry about missing that (it's even in the title!). ;)
This is because a prefab is not a part of any particular scene, so you can't attach gameobjects that belong to a specific scene to it. Imagine what would happen if you did, and then in another scene where the player gameobject might not exist, you try to instantiate a copy of the prefab. What Transform would it store in the variable? It can't know, because the gameobject doesn't exist anymore.
To fix this, you need to grab a reference to the player gameobject through code, I'm afraid. So you were right to begin with - you have to move to one of the GameObject.Find methods.
Try to add this, for example:
function Start ()
{
player = GameObject.Find("Player").transform;
}
Your answer
Follow this Question
Related Questions
Find transform.position of Another Game Object 1 Answer
Problems with simple AI script 3 Answers
Enemy Follow Player 1 Answer
Problem with trigger 1 Answer