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
2
Question by DoctorSauce · Jan 04, 2014 at 09:44 PM · randomgenerationorigin

How to change the origin of a gameobject?

I am working on some random generation, and before you laugh at my script, it's only a test for a very small part of the actual algorithm thingy. Anyway, I this script instantiates the hallway at the position of the object the script is attached to. But the trouble is, all the hallway objects have origins in the middle of them, so it spawns with the center at the gameobject. I want it to spawn at the edge of the game object, so I want the origin of the hallway to be at the edge of it. I can't just change the position it spawns at in the script, because each hallway is a different length and I am going to add a lot more. Hopefully you understand what I'm asking, basically I just want to change the point the gameobject spawns. If you need to see my script to understand what I'm asking about, here it is, but you might not need to look at it:

 using UnityEngine;
 using System.Collections;
 
 public class HallSpawner : MonoBehaviour {
     public GameObject redHall;
     public GameObject orangeHall;
     public GameObject yellowHall;
     public GameObject greenHall;
     public GameObject blueHall;
 
 
     private Vector3 spawnPos;
     private int hallChooser;
     private GameObject chosenHall;
 
     void Start(){
         spawnPos = (transform.position);
         hallChooser = Random.Range(1,6);
         if (hallChooser == 1){
             chosenHall = redHall;
         }
         else if (hallChooser == 2){
             chosenHall = orangeHall;
         }
         else if (hallChooser == 3){
             chosenHall = yellowHall;
         }
         else if (hallChooser == 4){
             chosenHall = greenHall;
         }
         else if (hallChooser == 5){
             chosenHall = blueHall;
         }
         /*else if (dungeonChooser == 6){
                     chosenDungeon = purple;
                 }*/
         else {
             Debug.Log ("You suck at coding!");
         }
         Instantiate(chosenHall,spawnPos, transform.rotation);
     }
 }
Comment
Add comment · Show 1
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 DoctorSauce · Feb 02, 2014 at 11:26 PM 0
Share

I found a ghetto way to fix it for my problem only, but I'm gonna leave this question up because I'm sure I'm not the only person who has had this question, and it would be great to have a universal fix for these problems rather than a screwy, acceptable fix for each individual problem.

1 Reply

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

Answer by Fabkins · Feb 03, 2014 at 12:11 AM

The easiest way is to create an empty object as a parent object. Then you put the corridor object as a child of the empty object. You can then alter the position of the corridor object so its positioned at the edge.

PS you might want to use a switch/case statement instead of a bunch of nested ifs. Or even better, put all in array if nothing special happens with each hall.

Comment
Add comment · Show 1 · 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 DoctorSauce · Feb 03, 2014 at 12:14 AM 0
Share

I really, really wish you had seen this question a long time ago and shared that answer, because that is freaking brilliant. I feel so, so, so stupid. But you rock, ima mark that as accepted. And yeah, I have since changed it to the array method. The script is much cleaner now, thank you for the advice.

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

19 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 avatar image

Related Questions

Make sure objects are not instantiated on each other 2 Answers

Randomly Generated Levels 3 Answers

How do I split a rectangle into random smaller rectangles? 1 Answer

How to create a perlin noise heightmap with the values being the highest around a point? 1 Answer

How to randomly generate non-intersecting cylinders within a circle? 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