- Home /
Prefab Cannot Select Object Correctly
I have an enemy that follows the nearest of 4 players, Red, Blue, Yellow, and Green. I created a prefab of all four, and the enemy and in the script I adress the Transform of those players, called red, blue, yellow, and gree, but because you cannot select a transform of a prefab, I created 4 extra GameObject variables they are redObj, blueObj, yellowObj, and greenObj. I selected the prefabs in the Inspector and set them to the Obj variables, and then set the variables of their transforms (red, blue, ...) and set them to redObj.transform ect. The problem is when I create instances of the players and enemy, the enemy still has the prefabs selected for the Obj variables, and cannot get their transform, but I want it to select the instances for the Objs. Ill post the script and a screenshot of the problem. I hope I'm being clear enough! Thanks! #pragma strict
public var myTransform : Transform; //current transform data of this enemy
public var target : Transform; // The transform of the player
public var redObj : GameObject;
public var yellowObj : GameObject;
public var greenObj : GameObject;
public var blueObj : GameObject;
private var red : Transform = redObj.transform;
private var yellow : Transform = yellowObj.transform;
private var green : Transform = greenObj.transform;
private var blue : Transform = blueObj.transform;
public var moveSpeed = 5.0; // the rate at which the enemy follows the player
public var rotationSpeed : float = 0.5; // the rate at which the enemy turns towards the player
public var activateRange : float = 25.0; // range at which enemy is activated
public var chaseRange : float = 55.0; // range at which the enemy follows the player once activated
public var dieRange : float = 1.0; // range at which the enemy blows up at the player. This is not really relevent for the question, sorry
public var activated : boolean = false; // if the enemy is activated or not
var standing : boolean = true; // makes sure that the force is not added multiple times, because this function is called every Update() frame
function Start(){
}
function Update(){
closestPlayer();
if (target == null){ // if there is no player
return;
}
var range: float = Vector3.Distance(target.position, transform.position); // calculates the distance between the player and the enemy
if(myTransform.rotation.eulerAngles.z == 90 || myTransform.rotation.eulerAngles.z == 270){
standing = false;
activated = true;
}
if(range <= activateRange){ // if the player is within activation range
Activate(); // then activate. see the Activate() function before continuing
}
if(activated){ // makes sure the enemy is activated
if (range <= dieRange){ // tests to see if the ennemy is within the range to attack the enemy
//Boom - still yet to do
}
else if (range <= chaseRange){ // if player is within the chase range, then chase
// Roatates towards the player
var relativePos : Vector3= myTransform.position - target.position; // finds the position of the player relative to the enemy
var rotateTo2 : Quaternion = Quaternion.LookRotation(relativePos, Vector3.up); // turns it into a Quaternion, which is basically a rotation
rotateTo2.eulerAngles = Vector3(0, rotateTo2.eulerAngles.y, 90); // sets it to only look at the y value (so it rolls on its side)
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, rotateTo2, rotationSpeed * Time.deltaTime); // turns to it
// Moves towards the player
myTransform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);
}
}
}
function Activate(){
var random = Random.Range(1,9); // picks a random number between 1 and 8
var xpos : float; // random x value for AddForce
var zpos : float; // random z value for AddForce
if(random == 1){ // if random == 1, choose random AddForce1
xpos = 0.4;
zpos = 0.4;
} else if(random == 2){ // ect.
xpos = -0.4;
zpos = -0.4;
}else if(random == 3){
xpos = -0.4;
zpos = 0.4;
}else if(random == 4){
xpos = 0.4;
zpos = -0.4;
}else if(random == 5){
xpos = 0.6;
zpos = 0;
}else if(random == 6){
xpos = -0.6;
zpos = 0;
}else if(random == 7){
xpos = 0;
zpos = 0.6;
}else if(random == 8){
xpos = 0;
zpos = -0.6;
}
if(standing){ // again makes sure it is onle added once
standing = false;
rigidbody.AddForce(xpos, 1, zpos * 500); // knocks over enemy onto side
yield WaitForSeconds (2); // waits until enemy is on ground before running the follow part of the script
activated = true; // activates the enemy
}
}
function closestPlayer(){
var rangeRed: float = Vector3.Distance(red.position, transform.position);
var rangeYellow: float = Vector3.Distance(yellow.position, transform.position);
var rangeGreen: float = Vector3.Distance(green.position, transform.position);
var rangeBlue: float = Vector3.Distance(blue.position, transform.position);
if(rangeRed < rangeYellow && rangeRed < rangeGreen && rangeRed < rangeBlue){
target = red;
return;
} else if(rangeYellow < rangeRed && rangeYellow < rangeGreen && rangeYellow < rangeBlue){
target = yellow;
return;
} else if(rangeGreen < rangeRed && rangeGreen < rangeYellow && rangeGreen < rangeBlue){
target = green;
return;
} else if(rangeBlue < rangeRed && rangeBlue < rangeYellow && rangeBlue < rangeGreen){
target = blue;
return;
}
}
I don't see Instantiate here anywhere, are you doing that elsewhere? Also "you cannot select a transform of a prefab" isn't clear are you meaning you want this
GameObject colorCube = Instantiate(prefabSource,pos,rot);
at which point you have the transform of the object you instantiated via colorCube.transform
I put this script on multiple enemies, meaning there can be up to like 25 at a time, so I dont want to use Instantiate() because that would create more than 4 players, which is not what I want. So I want to start by creating the players in the editor, and not with a script. I basically want the transforms of the players that I create within the editor. Before I created the Cylinder(the enemy) Prefab, I could select those transforms in the Inspector, but because I created a Prefab of Cylinder I cannot select the transforms or the objects in the scene from the Inspector. If you look inside the screenshot I took, inside the Select Gameobject window, there is no scene tab. Again to clarify, the end goal is to select the Transforms of the four players I created in the editor, Red, Blue, Yellow, and Green. Thanks!
Your answer
Follow this Question
Related Questions
Set a Prefab's variable when you instantiate it, but before it calls Awake? 2 Answers
Instantiate a Prefab thats been passed through 2 scripts? 2 Answers
Cannot assign prefab in Inspector? 2 Answers
Instantiate a prefab with a key press to a relative location? 1 Answer
Change Variable on Another Script 2 Answers