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 SuperSparkplug · Oct 09, 2013 at 09:45 AM · instantiatepositiongetcomponentanother scripttransform.

How do I instantiate at a certain point?

Hi, so I have a game where the player opens the door and it instantiates a prefab room on the other side on opening. I'm running into issues, however.

First off, I cannot get the prefab to instantiate at a certain position and angle, matching the door being opened. It always instantiates at 0,0,0. I've been reading the documentation on the instantiate property but the problem is that every time I put in the transform, quaternion, and other parameters I'm being told to include, Unity yells at me and I have no idea why when I'm pretty much doing it exactly as listed. I figure if I can make it inherit the transforms & rotation of the game object, it would mostly work and allow me to + or - for adjustments, but I can't seem to even get that to work since it won't let me call its transform properties or properly save them as a variable.

Second, I have no idea how to properly use the the GetComponent variable. Originally, I wanted to use a separate script to generate/instantiate the room by checking a variable in another script. The problem is I could never get the GetComponent function to work and have no idea how works, despite some examples I've found. To remedy this, I put the instantiation in the opening door script. It works (for now at least; it used to be spawning rooms infinitely) but it seems to be causing a glitch by making the door no longer open completely (It only moves a fraction of an amount its supposed to so it only twitches, and yes, I am taking into account the room being instantiated on top of it), so I think it may be best if its handled on another script.

Here's some code to illustrate the gist of what I'm doing.

     if(open){
         //Open door
         if (!opening) {
         Instantiate(Room_Prefab);
         transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth);
         open = true;
         opening = true;
         }
         
     }else{
         //Close door
         transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth);
         opening = false;
     }

Any ideas? I'm not very good with code yet. How would I write this if I wanted to put it in another script.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Tomer-Barkan · Oct 09, 2013 at 10:33 AM

To instantiate, this is the format:

 GameObject newRoomObject = (GameObject)GameObject.Instantiate(Room_Prefab, transform.position, Quaternion.identity);

then you have newRoomObject which is a reference to the newly instantiated GameObject. So you can use it to set any additional parameters, other than the position, of the new object.

To use GetComponent:

 Transform trans = newRoomObject.GetComponent<Transform>();
Comment
Add comment · Show 3 · 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 SuperSparkplug · Oct 09, 2013 at 06:02 PM 0
Share

I've tried both of those formats several times. All of them have issues and refuse to work, even though that, from what I can tell, it's perfectly correct.

avatar image Tomer-Barkan · Oct 09, 2013 at 07:48 PM 0
Share

Post your code when you use these formats, since I've used these hundreds of times without problems. And also any error messages you might get.

avatar image MarkD · Oct 10, 2013 at 07:43 PM 0
Share

As far as I could see, you wrote a C# script and i suspect he is using java. I posted the java version below.

avatar image
0

Answer by MarkD · Oct 09, 2013 at 07:58 PM

 Unity java:    
 
 var ExampleObj= new GameObject("ExampleObj")
     ExampleObj.GetComponent(Example component).its function here=anything needed;
     
     note that you do not need to place "" between the () if you try to get variables from scripts or unity components.
 
 Also all parameters on that new object can be entered or read.
 
 If you want to Add components on the fly you should replace GetComponent with AddComponent("Example component") Note that when adding components you do need the " ", because you are referring to a name of a component/script.
Comment
Add comment · 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

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

17 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

Related Questions

Not instantiating prefab in the right place after 2017 update 2 Answers

Keep instantiated GameObject's position while parenting it to another GameObject 3 Answers

spawn muzzle flash with random rotation? 2 Answers

Instantiate at original transform 1 Answer

Instantiated object coordinates from code aren't the same as the coordinates from Inspector?,Inspector Coordinates not the same as GameObject.Find Coordinates? 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