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.

43882-prefab.png

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.

43883-sceneview.jpg

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.

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.