-1
\$\begingroup\$

GameObject A (Panel - CraftSystem) is in the scene already as seen on the screenshot (Screenshot). I then instantiate the GameObject B (WorkingStation) which has a public GameObject craftSystem. So I need to assign A to B after B is instantiated.

this is the instantiated object code:

 using UnityEngine;
 using System.Collections;

 public class WorkingStation : MonoBehaviour
 {

     public KeyCode openInventory;
     public GameObject craftSystem;    //Game Object that needs to be assigned from hierarchy
     public int distanceToOpenWorkingStation = 3;
     bool showCraftSystem;
     Inventory craftInventory;
     CraftSystem cS;


     // Use this for initialization
     void Start()
     {
         if (craftSystem != null)
         {
             craftInventory = craftSystem.GetComponent<Inventory>();
             cS = craftSystem.GetComponent<CraftSystem>();
         }
     }

     // Update is called once per frame
     void Update()
     {
         float distance = Vector3.Distance(this.gameObject.transform.position, GameObject.FindGameObjectWithTag("Player").transform.position);

         if (Input.GetKeyDown(openInventory) && distance <= distanceToOpenWorkingStation)
         {
             showCraftSystem = !showCraftSystem;
             if (showCraftSystem)
             {
                 craftInventory.openInventory();
             }
             else
             {
                 cS.backToInventory();
                 craftInventory.closeInventory();
             }
         }
         if (showCraftSystem && distance > distanceToOpenWorkingStation)
         {
             cS.backToInventory();
             craftInventory.closeInventory();
         }
     }
 }

this is the code used to instantiate it:

 public void SetItem(GameObject b)
 {
     hasPlaced = false;
     currentBuilding = Instantiate(b).transform;
     placeableBuilding = currentBuilding.GetComponent<PlaceableBuilding>();
 }

Ive tried to do craftSystem = GameObject.FindGameObjectWithTag("CraftSystem"); but it does not work as after i instantiate the object B the public GameObject stays empty and i get null reference error to this line "craftInventory.openInventory();"

Ive also tried craftSystem = GameObject.Find("Panel - CraftSystem(Clone)"); and get the same result. public GameObject craftSystem still stays empty and i get null reference error.

\$\endgroup\$
  • \$\begingroup\$ I am not sure what your question is here. I guess you want to assign some public GameObject variable in your class PlaceableBuilding? Then it would be as trivial: placeableBuilding.variable = someGameObject. But maybe you wonder how to get the game object? In order to answer that question we would need to know more about your object hierarchy. \$\endgroup\$ – Philipp Jan 4 '17 at 12:19
  • \$\begingroup\$ The WorkingStation script has public GameObject and I want to assing an Object from hierarchy to it. \$\endgroup\$ – Barbas Jan 4 '17 at 12:24
  • \$\begingroup\$ Is the method SetItem in a class which has a reference to the game object which contains the craft system (or maybe it's even in that class)? And what do you mean with "from hierarchy"? From where in your hierarchy? \$\endgroup\$ – Philipp Jan 4 '17 at 12:29
  • \$\begingroup\$ SetItem is only used to instantiate the GameObject (a building) which is set in inspector. What i need to somehow assign is public GameObject craftSystem; From hierarchy i mean prnt.sc/drdhi3 \$\endgroup\$ – Barbas Jan 4 '17 at 12:35
  • 1
    \$\begingroup\$ if (craftSystem != null) ... I think you mean if (craftSystem == null) ("when you don't know where the craft system is, go find it"). \$\endgroup\$ – Philipp Jan 4 '17 at 14:39
0
\$\begingroup\$

If i understand correctly, you want to assign a GameObject (a prefab, let's call it A ) to another prefab, which you just instantiated (let's call it B).

There are 2 ways to do that :

  1. IF, the Prefab A is present in your game's hierarchy, then you can get it's reference using GameObject.Find or GameObject.FindGameObjectWithTag, and then assign it to your GameObject variable (craftSystem, in your case).

After Looking at the screenshot, I see that the Prefab A is instantiated at run-time and is not present in hierarchy at compile time (hence suffix clone).

Add the following line inside Start function, before anything else

craftSystem = GameObject.Find("Panel - CraftSystem(Clone)");

  1. IF, the Prefab A isn't present in the hierarchy, then you need to put that Prefab inside a folder called Resources, then call Resources.Load, on the saved Prefab (A), with this you'll get a reference to that prefab, which you can then assign to your craftSystem variable.

P.S. If this is not what you're looking for, then you need to make your question more clear, and i'll update my answer according to that.

\$\endgroup\$
  • \$\begingroup\$ Yes the GameObject A (Panel - CraftSystem) is in the scene already as seen on the screenshot. I then instantiate the GameObject B (WorkingStation) which has a public GameObject craftSystem. So I need to assign A to B after B is instantiated. \$\endgroup\$ – Barbas Jan 4 '17 at 12:47
  • \$\begingroup\$ Then you should try to use GameObject.Find or GameObject.FindGameObjectWithTag as I've said in my first point. \$\endgroup\$ – Aditya Yadav Jan 4 '17 at 12:53
  • \$\begingroup\$ Sorry If i misread or anything, but I don't see any screenshot attached to the question. \$\endgroup\$ – Aditya Yadav Jan 4 '17 at 12:54
  • \$\begingroup\$ it was attached to one of the comments: prnt.sc/drdhi3 I have tried using GameObject.Find and findwithtag but it doesnt seem to be working. craftSystem = GameObject.FindGameObjectWithTag("CraftSystem"); Unless this is totally wrong \$\endgroup\$ – Barbas Jan 4 '17 at 12:58
  • \$\begingroup\$ You need to have a tag on your CraftSystem - panel gameObject, only then the above method will work. Select CraftSystem Gameobject in the Hierarchy and in the Inspector window assign a Tag to it. \$\endgroup\$ – Aditya Yadav Jan 4 '17 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.