Author Topic: **Solved** Prefab script not working when instanciated  (Read 2915 times)

Lautaro

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 44
    • View Profile
**Solved** Prefab script not working when instanciated
« on: December 05, 2013, 06:28:28 AM »
Im instantiating a prefab like this:

   
  1.         public GameObject inventorySlotPrefab;
  2.  
  3.                 void Start ()
  4.                 {
  5.                                 var inventorySlot = NGUITools.AddChild (gameObject, inventorySlotPrefab);
  6.  
  7.                 }

The object appears on the screen. But the script doesnt work. I have this to try the script. ( i know it works cause i get the debug log when an instance of the prefab is dragged in on the screen in the editor)

   
  1. void OnClick ()
  2.                 {
  3.  
  4.                                 Component[] allComponents;
  5.                                 allComponents = gameObject.GetComponents<Component> ();
  6.                                 foreach (var component in allComponents) {
  7.  
  8.                                                 Debug.Log (component.name);
  9.  
  10.                                 }

Also, i see that some posts are marked *solved*. Do you want us to rename the posts when solved?
« Last Edit: December 05, 2013, 11:38:36 AM by Lautaro »

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Prefab script not working when instanciated
« Reply #1 on: December 05, 2013, 09:25:15 AM »
Regarding [solved]:
It's something some people do - I've renamed a few too, when they were resolved, but it's too much work to do. If you get a clear resolution of a problem, it would be nice though. :)

As for your issue, do you have a collider on the instance?
Is the InventorySlotPrefab properly referenced? (ie not missing)



Lautaro

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 1
  • Posts: 44
    • View Profile
Re: Prefab script not working when instanciated
« Reply #2 on: December 05, 2013, 11:38:10 AM »
Ok i will rename them. I appreciate the support forum alot.

I have realised that the code does run. But the reference to the camera that i drag dropped in the inspector is not saved with the prefab. Is this how Unity works?

Anyway so the camera reference was null and then the script didnt work.

I solved this by adding a public camera to my Globals class, referencing the NGUI camera to it and in the awake i set a static member called guiCamera to the NguiCamera. And then i used that in the prefab script like Globals.NguiCamera. And it worked.

Is it a normal problem and a normal solution?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: **Solved** Prefab script not working when instanciated
« Reply #3 on: December 05, 2013, 03:19:14 PM »
Prefabs can only reference components that are a part of the same prefab. They can't reference external components.