Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by pdunton · Jan 30, 2014 at 05:08 AM · javascriptprefab

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;
     }
 }

alt text

screenshot.png (168.0 kB)
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image getyour411 · Jan 30, 2014 at 08:44 AM 0
Share

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

avatar image pdunton · Feb 01, 2014 at 03:04 AM 0
Share

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!

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges