- Home /
Assigning gameobject to an instantiated prefab through script?
Hello, I'm creating a network multiplayer car game and i'm blocked by this issue of assigning checkpoint transforms to a cloned car. Let me explain it more so as to make it more clear, I have created cube as checkpoints and they are present in the hierarchy as CheckPoint1, CheckPoint2 etc.... Now once i start the game, i need to assign these checkpoints to a script attached to the cloned or instantiated prefab... Any help would be very helpful.
Answer by Loius · Apr 24, 2013 at 04:30 PM
GameObject.Find("Checkpoint1") will find an object named Checkpoint1 in the scene. I'm not sure if it checks inside object; you might want to make all your checkpoints the child of "CheckpointFolder" and then use GameObject.Find("CheckpointFolder/Checkpoint"+index).
well, thanks for that i got the idea but this script uses arrays and i'm just confused about how to assign those checkpoints
#pragma strict
var checkPointArray : Transform[]; //Checkpoint GameObjects stored as an array
static var currentCheckpoint : int = 0; //Current checkpoint
static var currentLap : int = 0; //Current lap
static var startPos : Vector3; //Starting position
function Start () {
//Set a simple visual aid for the Checkpoints
for (objAlpha in checkPointArray) {
objAlpha.renderer.material.color.a = 0.2;
}
checkPointArray[0].renderer.material.color.a = 0.8;
//Store the starting position of the player
startPos = transform.position;
}
this is basic stuff
var cp : GameObject;
var index : int = 0; // whatever your objects start at
var checkpoints : List.< Transform > = new List.< Transform >();
do {
cp = GameObject.Find("Checkpoints/Checkpoint"+index);
if ( cp == null ) break;
checkpoints.Add(cp.transform);
} while ( cp != null )
checkpointArray = checkpoints.ToArray();
Well, Im sounding quite noob i suppose, but i'm getting an error saying "The name 'List' does not denote a valid type ('not found'). Did you mean 'UnityEngine.Light'?"
corrected the error, but that code doesnt seem to attach those checkpoints to the script on the spawned car. Never $$anonymous$$d thank you
Answer by Lazy08 · Apr 24, 2013 at 04:13 PM
Let me see if I understand. You have cubes as checkpoints along the racetrack, and need a script(attached to a clone) to get their transforms? Try declaring a public GameObject checkpoint1, checkpoint2;
Then, from the editor, select the script on the assets tab. On the top of the Inspector tab, your checkpoints should appear. Assign the cubes to these so that the script will default to them. Then, simply attach the script to the clone car prefab. When instantiated, the clone should have the script attached, and the checkpoints should default to the right cubes.
well, i have a script attached to the clone, but i need to know how to attach these checkpoints to the script on this clone
don't put numbers after your variable names; use arrays. :/
Your answer
Follow this Question
Related Questions
Infinite Road Clone Duplication 1 Answer
How to Instantiate and change velocity 2 Answers
How could I save all cloned objects in a scene, and load them back up on start? 1 Answer
Instantiating a GameObject from scene that changes 1 Answer
What can I do to make the objects I instantiated move with the background? 1 Answer