Prefab and Script C# [ERROR]

Hi to all,

To call a Prefab object in the game when one action happens i’m using the following script:

public GameObject obj;
obj = Resources.Load(“ppoder”) as GameObject;
Instantiate(obj, transform.position, transform.rotation);
//Error message is: Failed to load component (MonoBehaviour)! Removing It!

And when i add this line of code, the change on the message is:

obj.AddComponent(“PMagia”);
//Error message is: Component MonoBehaviour could not be loaded when loading game object. Cleaning Up!
Component MonoBehaviour could not be loaded when loading game object. Cleaning Up!
UnityEditor.DockArea:OnGUI()

So i want to know if im using the right way to put a Prefab object in the game??
Any solution?

Thanks!

Simply use obj = Resources.Load(“ppoder”) as GameObject; and that’s it. Make sure your prefab is named “ppoder.prefab”. Then alter the obj gameObject like any other object: obj.transform.position = … (e.g.)
After that obj.AddComponent(“PMagia”); should work IF you have a script with that name (PMagia.cs).

Hi AxeIF,

I have it all working since the first post, but the Unity3D keeps telling me some messages… and when i restart it, or do another project with the same scripts he works fine till i use this line:

PMagia pm = obj.GetComponent();
pm.Seguir(transform);

I have this class called PMagia.cs, and the method Seguir(Transform selectedT);
And with this line he shows me this:
m_InstanceID == 0

But the project keep working fine… And i have started 3 projects to see if it’s keeps happening… any explain to this?

Maybe this thread will help: http://forum.unity3d.com/threads/68230-Unexplained-error-m_InstanceID-0

Hi AxeIF,

I think i found the error, i tried to put the SCRIPT PMagia in the Prefab when i created him, and the messages stoped… I guess i were doing something wrong trying to add a new Script in the Prefab, but to my project if the Prefab starts with the script it doesnt matter… Thanks for your help AxeIF.