How to spawn Prefab to a certain position.

Test scenario: 2D game. Lets say our hero runs to a magical area, stepping into that area spawns a magical door (a prefab) a certain number of units to the right. I need it to be independent to the spawning triggers location. That way I can put as many of them, and wherever I want them to be. After some searching I found a few scripts of instantiate, but they only spawn ON the trigger, not next to. I have experience in JavaScript. And I was pretty fluent, but haven’t touched it in about a year.

This is the simple code I’m using right now:

public var prefab : GameObject;
private var instObj : GameObject;

function OnTriggerEnter2D(other: Collider2D) {
	if(other.gameObject.name == "NLT"){
      Debug.Log("Box went through!");
     }
     
     instObj = Instantiate(prefab,transform.TransformPoint(Vector3.forward), Quaternion.identity);
}

Futhermore, while we’re here, I want to be able to randomly select from 3 TYPES of Doors, and then randomly from those 3 types. My line of thinking would be assign them to a class, then tell it to choose “randomly” from that class? (There’s probably a better method to it) But the last time I told code to do anything randomly was with C++ about 6 years ago.
I’ve dabbled (right now) Random.Range giving me an Int. Which I can use. i.e. Range of 1-4. Depending on if it gives me a 1/2/3, Thats what “class” of door I get. What I want to do next is choose 1 from 10 different doors in that specific class.

So, our hero runs into a magic spot, script chooses either door class 1, 2, or 3. Then, it chooses one door from 1-10 from that class. This interaction needs to happen randomly whenever our hero runs into a magic spot, no matter how many he runs into. When the door is chosen, it is spawned X units away. That’s my goal.

So if you kind people could help me start learning how to do that, I would be very glad.

Hi,
Your trigger function seems to be fine, it should spawn 1 unit away from the centre of the game object that has the trigger. However, TransformPoint is affected by scale so if your gameobject’s or its parent’s scale is not 1 on all the axis, you can get really strange results. In that case you shouldn’t use transformpoint.
Try this: (sorry, I’m more of a C# person so first I write it in C#)

Vector3 newPostion = new Vector3(transform.position.x + 1.0f, transform.position.y, transform.position.z);
instObj = (GameObject)Instantiate(prefab, newPosition, Quaternion.identity);

In Javascript it must be something like this:

var newPosition : Vector3;
newPosition = transform.position + Vector3(1,0,0);
instObj = Instantiate(prefab, newPosition, Quaternion.identity);

I’m not sure I understand the second part of your question, but I’ll try to come up with something.

You’re on the right track. I don’t know whether lists work the same in Javascript, but you could have a public collection of doors, and grab a random index using Random.Range(0, myDoors.Count);

assuming you always want these doors to spawn N number of units to the right of your trigger, you can select a random number N, then add it using transform.TransformPoint(Vector3.forward + N)