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 Ryunis · Dec 19, 2014 at 11:16 PM · c#instantiateprefabpositionrandom.range

Instantiating an Object doesn't use the given position.

Hi, I'm trying to Instantiate a prefab at a randomly generated position, but it always appears at the position the prefab had, when I originally created it. I already looked for similar questions, but I coulnd't find any... Anyways, here is my current code:

 using UnityEngine;
 using System.Collections;
 
 public class AppleManage : MonoBehaviour {
 
     public GameObject Apple;
 
     void Start ()
     {
         InvokeRepeating ("Spawn", 3f, 3f);
     }
 
     void Spawn () 
     {
         int s = Random.Range (1, 4);
 
         if (s == 1)
         {
             Vector3 position = new Vector3(Random.Range(-4.5F, 4.5F), Random.Range(-4.5F, 4.5F), 5.5F);
              Instantiate(Apple, position, Quaternion.identity); 
         }
 
         if (s == 2)
         {
             Vector3 position = new Vector3(5.5F, Random.Range(-4.5F, 4.5F), Random.Range(-4.5F, 4.5F));
             Instantiate(Apple, position, Quaternion.identity); 
         }
 
         if (s == 3)
         {
             Vector3 position = new Vector3(Random.Range (-4.5F, 4.5F), 5.5F, Random.Range(-4.5F, 4.5F));
             Instantiate(Apple, position, Quaternion.identity); 
         }
     }
 }
Comment
Add comment · Show 3
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 CorruptedTNC · Dec 20, 2014 at 08:18 PM 0
Share

I don't have unity in front of me so I can't test this, but if all else fails set the position of the object after it gets spawned, so:

GameObject go = Instantiate(Apple, position, Quaternion.identity) as GameObject; go.transform.position = position;

avatar image Ryunis · Dec 21, 2014 at 10:27 AM 0
Share

Thanks for your answer, but that doesn't seem to solve my problem either. Just to clarify what I did: I once created this Gameobject Apple at a specific location just to see how it looks inside my game.Then I created a prefab from it. But now whenever I try to instantiate this prefab, the original location is used and I don't know what to do...

avatar image Mmmpies · Dec 21, 2014 at 11:17 AM 0
Share

Use Debug.Log to show what position is and s, also use @CorruptedTNC suggestion to use GameObject go = then use Debug.Log to show go.transform.postion as well.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Ryunis · Dec 21, 2014 at 02:43 PM

Ok, looks like I'm simply an idiot. I had an animation attached to the prefab, which reset the position everytime. (I thought animation was local...) Thx anyway.

Comment
Add comment · Show 2 · 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 persijn · Dec 21, 2014 at 04:26 PM 0
Share

upvote for having the guts to post your error ins$$anonymous$$d of deleting your question.

avatar image bonnyychan · Mar 04, 2018 at 01:35 AM 0
Share

Thank you, I realize I was an idiot too ^^"

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiation in C# 1 Answer

instantiating vertically 2 Answers

How do I make prefab follow a gameobject?(C#) 1 Answer

Save gameobjects prefabs from scene A to a list, then instantiate those game objects to another scene. 0 Answers

Instantiate prefab once help 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