- Home /
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.
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>();
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.
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.
As far as I could see, you wrote a C# script and i suspect he is using java. I posted the java version below.
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.