- Home /
Get Object that Instantiated this object
I have a bullet script that I want to use the GameObject that created it. The bullet's creation:
GameObject projectile = (GameObject)Instantiate(bullet, transform.position, transform.rotation);
projectile.GetComponent<bulletController>().parent = gameObject; //Trying to set the GameObject variable parent
NetworkServer.Spawn(projectile);
The bullet uses it so that you can't shoot yourself. This is the current line, but ideally, I wouldn't need the GetInstanceID() because you can compare 2 game objects. if (hit.name.Contains("Character") && hit.GetInstanceID() != parent.GetInstanceID())
For some reason, you can still shoot yourself...
I dont know if it will work for your need.But you can try this.
While instantiating you have to define the parent. Instantiate(SelectedObstacle, Grid_Point_Position,GameController.transform.rotation,Obstacleholder.transform);
obstacle holder is my parent.
Thanks, but I'm not looking to set the actual parent, only looking to identify the GameObject that instantiated this GameObject.
Answer by OrbitalTech · Dec 05, 2017 at 08:19 AM
I ended up moving the whole system to the player script. It hasn't fixed the issue, but now I don't have to identify the instantiating object.