- Home /
Instantiated Prefabs Not Showing Components
I have been looking around for about an hour now and can't find anything like this with replies, so I figured I'll give this a shot.
Essentially, my prefab won't instantiate with expected components in runtime.
Below is my prefab within its folder in the assets section (with all the expected components & references attached):
And below is my prefab upon creation during runtime:
Below is the code I've used to instantiate the object:
public void InstantiateNewDog(Vector3 vector3, Quaternion quaternion, Transform transform, int dogType)
{
GameObject newDog = Instantiate(dogObject, vector3, quaternion, transform);
DogController newDogController = newDog.GetComponent<DogController>();
newDogController.iDogID = dogType;
}
The code instantiates the dog object at the requested position and parents it to an object, then attempts to connect to the DogController script (which is connected to the prefab in the assets folder), however in runtime this component does not exist and therefore doesn't work.
I'm really quite stuck with this. I've used virtually the same code on a previous project and it worked, but this time it doesn't work. Additionally, it doesn't look like a lapse in save data as I never saved the prefab without a reference to the Dog Controller instance. It's also puzzling because the Event Trigger script is still available in runtime, however, loses its references.
If anyone can tell me if this is unusual behaviour or if this is expected and I'm doing something wrong that would be much appreciated :)
did you double check everything? because this is very odd
I have. $$anonymous$$y only thoughts are that it could be an issue with that one instance of unity being open, or i should delete and re-create the prefab.
It is really bizarre
Answer by IsaacShea · Jan 27, 2020 at 06:22 AM
For anyone wondering, I just deleted and re-created the prefab and everything seems to be functioning fine.
This was almost definitely a bug that seems to be surrounding the saving of prefabs, but I'm not sure how to recreate it to file a bug report so I'll just let it sit for now.
I've been working on the project overall and I found out what was wrong with it. There was a line in the code that was responsible for deleting the dog if it was an "overflow" (which the ones created using code were mistakenly classed as), but ins$$anonymous$$d of having Destroy(this.gameObject)
I missed .gameObject
, which caused it to "delete" itself but leave the object, causing weird behaviour.
This is not a bug on Unity's end as it turns out, it's just me forgetting pieces of my code.
I'm glad you found it. I spent some time with this over the weekend trying to figure out what the issue could be. Thanks for updating the post. It may be helpful to someone in the future.
Your answer
Follow this Question
Related Questions
Instantiated prefab loses components 1 Answer
prefab is instantiating without a script 2 Answers
Prefabs dont instantiate directly on point 0 Answers
Referencing gameObject from script after Instantiate 0 Answers
Prefab of a whole scene? 3 Answers