Hey, so is there an easy way to select where an object will be instantiated?
EDIT: What i meant is if it can be selected where to instantiate it in the hierarchy?
Hey, so is there an easy way to select where an object will be instantiated?
EDIT: What i meant is if it can be selected where to instantiate it in the hierarchy?
Yes. There is an overloaded version of Instantiate that takes a Vector3 and a Quaterion as arguments. The Vector3 defines the position of the new GameObject.
Yes and No. (EDIT: Saw you update question, and yes you can either find that gameobject in hierarchy using Gameobject.Find(“Name”) and instantiate it on its position (if you want it to be instantiated there), or after instantiating a gameobject you can make it a child of another object, this is done by using Transform.Parent)
You can pre-define positions at which you will be able to instantiate objects
For example, Place and empty Gameobject (called something like SpawnPoint), then in script instantiate something on that position, so…
public Gameobject spawnPoint; //reference the SpawnPoint Gameobject
Gameobject objectToSpawn;
void Start()
{
//Instantiate that Gameobject on the spawnpoint position
Instantiate(objectToSpawn,spawnPoint.transform.position,Quaternion.Identity);
}
Another way is to make an empty Gameobject, and instantiate objects randomly around that gameobject, maybe up to distances of 10 or whatever is desired by using Random.Range(float,float) function.
There is many ways in how you can choose where to instantiate objects.
You could create an empty game object (essentially a spawnpoint) and instantiate your object at that object’s position. quill18’s multiplayer FPS has a good video on this.
That said, here's a basic way to do it. Create an empty object called spawnpoint at the location you want to spawn. This will be invisible to the player but serve as a reference point to spawning our object. My script is setup for multiplayer in Photon but you should be able to figure it out for your intended use. Now in your network manager script (if you don't have one, make it and add it to an empty game object called Network Manager or something), do this: using UnityEngine;
using System.Collections.Generic;
public class NetworkManager : MonoBehaviour {
public GameObject spawnpoint;
void Start(){
// This line is finding your spawnpoint game object so we can use it
spawnpoint = GameObject.Find ("spawnpoint");
// This runs our instantiate function, you don't have to do this on start, I'm just doing this as an example
InstantiateMyObject();
}
void InstantiateMyObject() {
// This could simply be placed in Start()
GameObject Object = PhotonNetwork.Instantiate ("yourObjectNameHere", spawnpoint.transform.position, spawnpoint.transform.rotation, 0);
}
}
yourObjectNameHere should be the object you want to instantiate, and it might have to be a in Prefabs/Resources folder. I’m not sure I’m pretty new to this, check out the video for more info.