- Home /
How to assign Transform to prefab ?
I've been working for 2 days and i still didn't succeed :(
There's 2 spaceships i use...
and ther are using same code;
I wrote a smooth look at code using random methods. Bu I can't define my transform objects to prefab.
As you see;
I'm trying to drag drop them but it's not available.
var targetsArray : Transform[];
var damping = 6.0;
var smooth = true;
private var target = -1;
function Start() {
target = Random.Range(0, targetsArray.Length);
}
function LateUpdate () {
if (smooth)
{
// Look at and dampen the rotation
var rotation = Quaternion.LookRotation((targetsArray[target]).position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
}
and that's my code. So , Is there another method can i use ?
P.S. = I'm using 1 scene in my game. One level, one scene.
Answer by robertbu · Feb 01, 2014 at 04:19 PM
A Prefab is like a blueprint for a game object (including scripts and children). It doesn't exist in the scene until it is Instantiated(). You cannot drag and drop scene items onto a prefab since it doesn't exist in the scene and can be Instantiated into any scene. You can drag and drop other object within the prefab...that is you can fixup internal links within the prefab, just not external ones.
As for your problem, you are going to have to figure out an alternate way to make these links. Something like:
var targetsArray : Transform[] = new Transform[4];
var damping = 6.0;
var smooth = true;
private var target = -1;
function Start() {
targetsArray[0] = GameObject.Find("1Follower").transform;
targetsArray[1] = GameObject.Find("2Follower").transform;
targetsArray[2] = GameObject.Find("3Follower").transform;
targetsArray[3] = GameObject.Find("4Follower").transform;
target = Random.Range(0, targetsArray.Length);
}
An alternate method would be to use the tag manager to give all the followers the same tag. Then you could do:
var targetsArray : GameObject[];
var damping = 6.0;
var smooth = true;
private var target = -1;
function Start() {
targetsArray = GameObject.FindGameObjectsWithTag("Follower");
target = Random.Range(0, targetsArray.Length);
}
Note this alternate method has the entries of the 'targetsArray' as game object, not transforms.
This approach does work perfectly fine for the question that was asked here. If it doesn't work in your case, you don't have the same problem. This question is 6 years old and the code is written in UnityScript which is no longer supported in the latest Unity versions. Regardless of the used language the concept does still work the same way. Your comment is just a pointless bump of an ancient, answered question.
Answer by Spark59op · Apr 11, 2021 at 07:16 PM
Ok try this... It worked for me For example i am using my transformable object "player" with tag "Player"
First give reference
GameObject player;
Then in start function
player = GameObject.FindWithTag("Player"); target = player. transform;
It will work perfectly