• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Joeltron · Aug 01, 2013 at 08:07 PM · c#instantiateprefabtagclone

Instantiated clones arent behaving the same as the prefab

(video of problem: link text)

I have a prefab that is tagged rotatewithPlayer, when I instantiate the prefab, they are positioned randomly, but are at set heights, and face the camera. Here is my code snippet.

for (i = 0; i < maxTrees; i++){ randX = Random.Range(minX,maxX); randZ = Random.Range(minZ,maxZ); Instantiate(tree1, new Vector3(randX,12.2f,randZ), transform.rotation);

and in my player controller script, i iterate through an array of all the objects that are tagged rotatewithPlayer and turn them 90 degrees accordingly. And the code snippet...

 Start () {
         controller = GetComponent<CharacterController>();
         rotate_WithPlayer = GameObject.FindGameObjectsWithTag("rotateWithPlayer");
     }//end start
     
     void Update () {
         //some code
         if (Input.GetKeyDown("q")){
             //rotate tagged objects 90deg left
             foreach (GameObject x in rotate_WithPlayer){
                 x.transform.Rotate(0,-90,0,Space.World);
             }
         }if (Input.GetKeyDown("e")){
             //rotate tagged objects 90deg right
             foreach (GameObject x in rotate_WithPlayer){
                 x.transform.Rotate(0,90,0,Space.World);





` All of the tagged objects, when placed in the scene manually will rotate with the player. but the ones instantiated by the above script do not rotate at all.

The game im making is 2d in a 3d world, and its constrained to 90degree turns. every object is a texture on a plane. when the player turns right, all of the tagged objects should rotate 90 degrees in place so that they are all still visible to the player(camera). The script seems to work properly on the manually placed prefabs, but not the instantiated ones. Also, I've found that if I copy/paste my movement script into a new script file and use that on my the player, that the instantiated object will then rotate with the player as intended, but when unity regains focus (after i switch to another window) the instantiated objects dont rotate. im not sure why it seemingly works, then doesnt. any help or insight would be greatly appreciated. ps. im a newbie so, if i havent provided enough info please excuse me, and if its something really simple, then i appologize

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Seizure · Aug 01, 2013 at 08:44 PM

Its very difficult to determine your issue from the given, what I gather is that they instantiate at an odd rotation but then continue on with the rotation script as intended? If that is the case I think its because of your

 Instantiate(tree1, new Vector3(randX,12.2f,randZ), transform.rotation);

Try this:

 Instantiate(tree1, new Vector3(randX,12.2f,randZ), Quaternion.Identity);

The quaternion.identity will apply the prefabs rotation whereas transform.rotation was using the rotation of whatever gameobject the script is attached to.

Comment
Add comment · Show 4 · Share
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 Joeltron · Aug 02, 2013 at 04:11 AM 0
Share

I have tried and changing the code doesn't do what I need. When i use Quaternion.identity, all of the object spawn parallel to the ground, while the manually placed prefabs in the scene are standing horizontally.

avatar image VioKyma · Aug 02, 2013 at 05:29 AM 0
Share

Seizure has given most of the answer. Just add a Quaternion variable, adjust the rotation from the Identity and then apply the variable.

 Vector3 myVector = // The direction I want to face
 Quaternion myRotation = Quaternion.Identity;
 myRotation.LookRotation(myVector);

Then use:

 Instantiate(tree1, new Vector3(randX,12.2f,randZ), myRotation);
avatar image Joeltron · Aug 02, 2013 at 11:19 PM 0
Share

trying this doesn't work for me either. the objects are instantiated, but do not rotate with the player like prefabs i have manually in the scene. i made a brief video showing the problem. please excuse my poor video editing skills.link text

here is my spawner code: using UnityEngine; using System.Collections; public class spawner : $$anonymous$$onoBehaviour { public GameObject tree1; public GameObject tree2; public GameObject bush; public GameObject shrub; public GameObject rock1; public GameObject rock2; public GameObject stump; public GameObject grass; static float randX; static float randZ; static int $$anonymous$$X = -94; static int $$anonymous$$Z = -95; static int maxX = 54; static int maxZ = 52; static int i; static int maxTrees = 300; static int maxBushes = 100; static int maxShrubs = 100; static int maxRocks = 75; static int maxStumps = maxTrees/6; static int maxGrass = 600; // Use this for initialization void Start () { //trees for (i = 0; i < maxTrees; i++){ //tree1 randX = Random.Range($$anonymous$$X,maxX); randZ = Random.Range($$anonymous$$Z,maxZ); Instantiate(tree1, new Vector3(randX,12.2f,randZ), transform.rotation); //tree2 randX = Random.Range($$anonymous$$X,maxX); randZ = Random.Range($$anonymous$$Z,maxZ); Instantiate(tree2, new Vector3(randX,14.6f,randZ), transform.rotation); }//end trees //etc for each object }//end start // Update is called once per frame void Update () { }//end update }//end class

.

avatar image Joeltron · Aug 02, 2013 at 11:20 PM 0
Share

and here is the script i have attached to my player: using UnityEngine; using System.Collections;

 [RequireComponent(typeof(CharacterController))]
 public class ball$$anonymous$$ove : $$anonymous$$onoBehaviour {
 
     static Vector3 move;
     float speed = 10f;
     CharacterController controller;
     GameObject x;
     GameObject[] rotate_WithPlayer;
                 
     void Start () {
         controller = GetComponent<CharacterController>();
         rotate_WithPlayer = GameObject.FindGameObjectsWithTag("rotateWithPlayer");
     }//end start
     
     // Update is called once per frame
     void Update () {
         move = new Vector3(Input.GetAxis ("Horizontal"), 0f, Input.GetAxis ("Vertical"));
         move = transform.TransformDirection(move);
         controller.Simple$$anonymous$$ove(move * speed * Time.deltaTime * 10);
         if (Input.Get$$anonymous$$ey("space")){
             speed = 20f;
         }else{ 
             speed = 10f;
         }if (Input.Get$$anonymous$$eyDown("q")){
             //rotate tagged objects 90deg left
             foreach (GameObject x in rotate_WithPlayer){
                 x.transform.Rotate(0,-90,0,Space.World);
             }
         }if (Input.Get$$anonymous$$eyDown("e")){
             //rotate tagged objects 90deg right
             foreach (GameObject x in rotate_WithPlayer){
                 x.transform.Rotate(0,90,0,Space.World);
             }
         }
         
     }//update
         
 }//class

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

16 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

Related Questions

How do I make a clone of a prefab appear on the correct layer? [5.2.2f1] 1 Answer

Instantiate prefab (button, texture) with attached script 1 Answer

Having multiple objects fire prefabs in different times C# 0 Answers

Multiple Cars not working 1 Answer

instantiate prefab assigned in inspector c# 1 Answer


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