Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Instantiate a prefab and assign it's values

Discussion in 'Scripting' started by RickyX, Jan 13, 2016.

  1. RickyX

    RickyX

    Joined:
    Jan 16, 2013
    Posts:
    280
    I really need to assign the values of my prefab right after instantiating it. The source of my prefab is from assets, not from hierarchy.
    I do something like this:
    Code (csharp):
    1.  
    2. public GameObject boxPrefab;
    3. public void InstantiateBox()
    4. {
    5.         GameObject copy = (GameObject)Instantiate(boxPrefab, transform.position, Quaternion.identity);
    6.         copy.GetComponent<TestBoxScript> ().varToAssign = gameObject;
    7. }
    8.  
    But the second line does not apply at all for some reason...
    The gameobject is instantiated but it's varToAssign does not change at all.

    I really don't know how to approach my problem if this is not possible to do. And yes i tought of assigning the variable on Start() of my TestBoxScript but that's not gonna do what i want. I want my prefab to find it's belonger player, and if i did it from it's own script, it would never know which player it belongs to, if that makes sense.
    Also, this is not the original code i have, becouse i want my code to be private. But it's the same thing, it won't apply the second line in the function...
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    well the code you've posted works without an problems here... but if it isn't the actual code, that probably doesn't help.
     
  3. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    So your TestBoxScript class has a public variable called varToAssign which is of the type GameObject?
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    if the variable doesn't exist as a public variable you should be getting an error in the scripting ide and unity, if the script isn't on the instance of the prefab you should be getting a null ref error at runtime...
     
  5. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    Yup.
     
  6. RickyX

    RickyX

    Joined:
    Jan 16, 2013
    Posts:
    280
    Yes man, correct.


    I'm sure it's the same. Nothing could have coused the second line not to work, like, that's just so random. I really hate showing my original codes, not becouse someone is stealing my work, that's stupid, mine wouldn't be useful to anyone else, but it's just idk. But if you really think it is, here:


    Code (csharp):
    1.  
    2. public void InstantiateInventoryItem(int itmID)
    3.     {
    4.         GameObject clone = (GameObject)Instantiate(ItemDB.GetComponent<ItemDatabase>().items[itmID].itemDragDropPrefab, BarBlood.transform.position, Quaternion.identity);
    5.  
    6.         clone.GetComponent<ItemCollisionDetection> ().itemDataBase = ItemDB;
    7.         clone.transform.parent = UnReadyItemsUI.transform;
    8.         clone.GetComponent<ItemCollisionDetection> ().Inv = UnReadyItemsUI;
    9.         clone.GetComponent<ItemCollisionDetection> ().CharCont = gameObject;
    10.         //clone.transform.position = transform.position + new Vector3 (0,40,0);
    11.  
    12.     }
    13.  
    Code (csharp):
    1.  
    2. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    3.      
    4.         RaycastHit h;
    5.         if (Physics.Raycast (MC.transform.position, ray.direction, out h))
    6.         {
    7.             if (Input.GetKeyDown(KeyCode.Mouse0))
    8.             {
    9.                 if(h.transform.gameObject.GetComponent<Pickup>() == true)
    10.                 {
    11.                     gameObject.GetComponent<Stats> ().InstantiateInventoryItem (h.transform.gameObject.GetComponent<Pickup> ().GiveItemID);
    12.             GameObject.Destroy (h.transform.gameObject);
    13.                 }
    14.             }
    15.         }
    16.  
    17.  

    And yes it gets instantiated but every single thing i tried to assign does not apply neither does it give an error.


    EDIT: Thoose are not full codes btw. i really can't post the full codes online.
     
    Last edited: Jan 13, 2016
  7. RickyX

    RickyX

    Joined:
    Jan 16, 2013
    Posts:
    280
    Oh damnn, i got it guys @SteveJ @LeftyRighty , the thing was, instantiate picks anything you give it, it doesen't need to be a GameObject, i think atleast. the reference that i got from my item database for the prefab was a transform, that's why it didn't apply my lines.
    Code (csharp):
    1. ItemDB.GetComponent<ItemDatabase>().items[itmID].itemDragDropPrefab
    itemDragDropPrefab was a transform so yeah, it took that and spawned it, and i couldn't assign anything becouse i did clone.GetComponent instead of clone.gameObject.GetComponent
    So it was my fault, but thanks for checking out my thread and taking your time anyways.