How do you pick up an instantiated object?
Hello! So I have the standard code to pick up an object within the scene:
public class PickUp : MonoBehaviour
{
public Transform ObjectPos;
void OnMouseDown()
{
GetComponent<Rigidbody>().useGravity = false;
this.transform.position = ObjectPos.position;
this.transform.parent = GameObject.Find("ObjectPosition").transform;
}
void OnMouseUp()
{
this.transform.parent = null;
GetComponent<Rigidbody>().useGravity = true;
}
}
My issue is that you can't assign a transform to a prefab object because it hasn't been spawned yet. I have a series of objects that spawn during Runtime that I need the player to be able to pick up. This seems like it should be super simple but I can't find any information on it at all.
Answer by Hellium · Mar 16, 2020 at 11:17 AM
Reference the ObjectPos
object from the class responsible for spawning the PickUp objects and inject the reference when instantiating the objects
public class PickUpSpawner : MonoBehaviour
{
public PickUp PickUpPrefab;
public Transform ObjectPos;
void SpawnPickUp()
{
PickUp pickUp = Instantiate(PickUpPrefab);
pickUp.ObjectPos = ObjectPos;
}
}
That works! Thank you very much. So now Unity is obviously throwing an error because the PickUp script hasn't been given a Transform for ObjectPos, however if I remove that line of code, I get the following error:
CS1061 'PickUp' does not contain a definition for 'ObjectPos' and no accessible extension method 'ObjectPos' accepting a first argument of type 'PickUp' could be found (are you missing a using directive or an assembly reference?)
Surely now that ObjectPos is being referenced within the PickUpSpawner script, the old one is redundant?
You must keep public Transform ObjectPos;
inside your PickUp
class.
So now Unity is obviously throwing an error because the PickUp script hasn't been given a Transform for ObjectPos
Can you provide the exact error thrown by Unity? Is it a NullReferenceException
? If so, you may have forgotten to call pickUp.ObjectPos = ObjectPos;
after instantiating the object. (make sure to inject the value inside the instanciated object, not inside the prefab)
So right now, I have the following code within my PickUpSpawner script, attached to the spawner:
public PickUp PickupPrefab;
public Transform ObjectPos;
public void SpawnBox()
{
PickUp pickup = Instantiate(PickupPrefab);
pickup.ObjectPos = ObjectPos;
}
Then, in the PickUp script attached to the prefab I have the following:
public Transform ObjectPos;
void On$$anonymous$$ouseDown()
{
GetComponent<Rigidbody>().useGravity = false;
this.transform.position = ObjectPos.position;
this.transform.parent = GameObject.Find("ObjectPosition").transform;
}
void On$$anonymous$$ouseUp()
{
this.transform.parent = null;
GetComponent<Rigidbody>().useGravity = true;
}
}
The Pickup Prefab and Object Pos are then referenced correctly. But by keeping the line public Transform ObjectPos;
within the PickUp script, attached to the prefab, I'm getting the NullReferenceException error because I can't give the prefab the transform it wants. Have I got the PickUp script in the right place? Should it not be attached to the prefab?
Your answer
Follow this Question
Related Questions
Problema al crear objeto por segunda vez. Problem to instantatiate a object for second try. 1 Answer
Object reference not set message, directly after instantiation 1 Answer
Can't set transform.position AND constantforce.force? 1 Answer
Falling object respawner 1 Answer
Wrong position (always 0,0,0) when Instantiate prefab. 1 Answer