- Home /
How to attach a Script/GameObject in the Scene view to a Prefab in the Assets folder.
I have a Bullet prefab which has a Gravity Script attached to it, it requires a Planet GameObject to be plugged in.
When the game is started with the bullet in the scene (not in the resources folder) Attractor has a the planet attached to it, which looks like this.
The Planet object is a gameobject in the scene view, however when the prefab is in the resources folder (Where I need it) it won't allow me to plug in the Planet.
Planet has a Script called GravityAttractScript to it, and I think that is what I need to access, however I will need the Planet Gameobjects transform for the script to work.
My question is, how do I either get the Planet to plug in to the GravityBodyScript while it is in the resources folder, or how do I get to plug the planet when the bullet is instantiated?
Here is the relevant script for GravityBodyScript that I want to have the planet properties attach to.
public GravityAttractScript planetAttractor;
private GameObject planetObject;
void Start()
{
originTransform = GameObject.Find("OriginLimiter");
planetObject = GameObject.Find("Planet");
}
So to reiterate, this code works when the bullet is already in the Hierarchy/Scene View before I start the game, but not when the game is instantiated from a prefab, I tried changing the code to Awake() instead of Start(), and also including Awake(), but that field never gets the information.
Answer by hbalint1 · Apr 04, 2015 at 11:43 AM
When you instantiate the object it has some specific name like: Planet(Clone). Try to change the Find method to:
planetObject = GameObject.Find("Planet(Clone)");
I'm not sure why you can't drag and drop from the Resources. Change private to public. After that you should be able to drag and drop it.
The planet object is already in the scene view and isn't being instantiated or cloned, I'm only instantiating the bullet.
So I do have lots of Bullet(Clone) objects, but none of them have the planet plugged in.
Here is the code for the bullet being instantiated.
//This is where the bullet object is plugged in
public Rigidbody bulletRigidbody;
void ShipFiring()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
Rigidbody bulletClone;
//Instantiate bulletRigidbody in front of object ($$anonymous$$aybe I could set the bullet clones planetObject here somehow? Not sure how to do that)
bulletClone = Instantiate(bulletRigidbody, gunPlaceholder.position, transform.rotation) as Rigidbody;
//$$anonymous$$ove Bullet Forward
bulletClone.velocity = transform.TransformDirection(new Vector3(0,0,bulletSpeed));
//Destroy the bullet after a few seconds
Timer(bulletClone);
}
}
so if you have only one planet why do you want to set it like this? planetObject = GameObject.Find("Planet");
If you have the prefab at the Resources, just set the private GameObject planetObject;
to public and drag and drop the planetObject from the scene to the prefabs planetObject. So it will automatically get it right after instantiate and you don't need to Find() it.
I ended up using a getcomponent to get the script from the planet that I needed, and I set the planet object to public just to make it easier, and now it is working, for those who want to know how I did it, here is the code.
planetAttractor = planetObject.GetComponent<GravityAttractScript>();
So turns out you can't just try and plug the planet object into the GravityAttractScript socket, this method at least allows you to access the object and script.
Thanks for the help man!
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Prefabs spawn with wrong values 0 Answers
Multiple Cars not working 1 Answer
How to make TCG Deck (Instatiate based on Prefab) 1 Answer
Animator is not playing a Playable 2 Answers