- Home /
When adding a script using GetComponent<>(), some prefabs that are assigned don't come up
I'm not sure why it's doing this, but I currently have a script that only has this:
// Use this for initialization
void Start ()
{
gameObject.AddComponent("TestStraightShooter");
}
The script that I'm adding has a prefab component to it that's assigned in the inspector.
Like so...
However when I run the game, the script gets added to the game object, but the Prefab on the script isn't set to anything. Like so...
If I just drag the script in, the prefab is there, but if I try doing it via code, it doesn't show up.
I'm doing this because in my game, you'll be able to purchase different weapons which are just going to be scripts added to the player game object.
Answer by liszto · Dec 28, 2012 at 01:11 PM
After your :
void Start ()
{
gameObject.AddComponent("TestStraightShooter");
//If you do this it doesn't work ?
//Create your prefab instance here for example
gameObject.GetComponent<TestStraightShooter>().bulletPrefab = oneInstanceOfYourPrefab
}
I think you can find all answers to your questions here
Okay. I had this idea before, but I couldn't do it cuz I couldn't figure out how to assign it to an instance of the prefab. Like how do I instantiate it?
I have the prefab in my Project tab, is that what I should be referencing? I'm not exactly sure how though.
I don't remember if edit an answer notify you so I edit my answer look the link and search in Rockets & Explosions part.
If you need more don't hesitate ;)
Your answer
Follow this Question
Related Questions
Assign object in hierarchy as public variable in prefab 1 Answer
Why cannot I drag and drop a gameObject from Hierarchy to a prefab's slot in the Inspector? 3 Answers
How to instantiate a prefab after a certain action happened? 2 Answers
Defining a public variable with 'where' 2 Answers
Calling individual variables from individual scripts 1 Answer