Search Unity

How to put prefab without dragging it?

Discussion in 'Scripting' started by Atix07, Jul 24, 2014.

  1. Atix07

    Atix07

    Joined:
    Jul 27, 2013
    Posts:
    182
    Hello! So I got some problems about this. I made this code but ıts not working. Where is the problem ? Thanks!

    Code (csharp):
    1.  
    2. GameObject SodaPrefab = (GameObject)Resources.Load("/SodaCanPickUp");
    3.  
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You're trying to place it in the scene? You need to Instantiate it. Also (this is probably your bigger problem), when using Resources.Load, don't use a leading /.
    Code (csharp):
    1.  
    2. GameObject SodaPrefab = (GameObject)Resources.Load("SodaCanPickUp");
    3. GameObject SodaInScene = (GameObject)Instantiate(SodaPrefab);
    4.  
     
  3. Atix07

    Atix07

    Joined:
    Jul 27, 2013
    Posts:
    182
    @StarManta So code is like this... When I press G I want to put prefab ın the scene. But I cant drag the prefab ın the hiercay because I have lots of clone of this code. So thats why ı want to use resources.load

    Code:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Soda : MonoBehaviour
    6. {
    7.     private vp_FPInventory m_Inventory = null;
    8.     private PlayerAyar m_PlayerAyar = null;
    9.  
    10.     public Transform Player;
    11.  
    12.     GameObject SodaPrefab = (GameObject)Resources.Load("/SodaCanPickUp");
    13.  
    14.     // Use this for initialization
    15.     void Awake ()
    16.     {
    17.         Player = GameObject.FindGameObjectWithTag("Player").transform;
    18.         m_Inventory = Player.transform.GetComponent<vp_FPInventory>();
    19.         m_PlayerAyar = Player.transform.GetComponent<PlayerAyar>();
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update ()
    24.     {
    25.         vp_ItemInstance currentObject = m_Inventory.CurrentWeaponInstance as vp_ItemInstance;
    26.      
    27.         if (Input.GetMouseButtonDown(0))
    28.         {
    29.             m_PlayerAyar.Susuzluk = m_PlayerAyar.Susuzluk - 20;
    30.             m_Inventory.TryRemoveItem(currentObject);
    31.         }
    32.         else if (Input.GetKeyDown(KeyCode.G))
    33.         {
    34.             Rigidbody clone;
    35.             clone = Instantiate(SodaPrefab, transform.position, transform.rotation) as Rigidbody;
    36.             clone.velocity = transform.TransformDirection(Vector3.forward * 10);
    37.         }
    38.     }
    39. }
    40.  
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Ah, yeah, having this as a member definition changes things. There are fewer options for how to set the values in those. Basically, anything that involves any sort of processing won't work.

    Code (csharp):
    1.  
    2. GameObject SodaPrefab
    3.  
    4. void Reset() {
    5. SodaPrefab = (GameObject)Resources.Load("SodaCanPickUp");
    6. }
    7.  
    You'll have to right-click on the component and hit "Reset" when you first do this, but when you add this component to new objects, Reset() is run automatically as soon as the component is attached, so it will basically have that as a default value.

    You should probably call Resources.Load around the time you are going to instantiate the prefab, however. Unless certain objects can "override" what they think a soda can is, there's no reason to store the prefab reference this way, and it'll just cause annoyance later on.
     
  5. Atix07

    Atix07

    Joined:
    Jul 27, 2013
    Posts:
    182
    @StarManta Actulay ım trying to do DropWeapon key. When the G button is pressed I want to create a prefab on the scene and destroy the weapon on the hand. I can destroy the weapon on the hand but ı cant drop weapon to the floor. Do you have any idea?
     
    Last edited: Jul 24, 2014
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I don't understand your question?
     
  7. Atix07

    Atix07

    Joined:
    Jul 27, 2013
    Posts:
    182
    @StarManta I got Eror. Its says: The thing you want to instantiate is null.

    Code (csharp):
    1.  
    2. GameObject SodaPrefab = (GameObject)Resources.Load("SodaCanPickUp");
    3.  
    Why I cant find the prefab with this object? It says ıts null.
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Is there an object of that name in the root Resources folder? (Screenshot?)
     
  9. Atix07

    Atix07

    Joined:
    Jul 27, 2013
    Posts:
    182

    Attached Files:

  10. Atix07

    Atix07

    Joined:
    Jul 27, 2013
    Posts:
    182
    @StarManta Okay ı put debug on it and now it says : Soda prefab is not null/ Prefab is found.
    So I think there is problem about Instantiate.

    Code (csharp):
    1.  
    2. GameObject SodaPrefab = (GameObject)Resources.Load("SodaCanPickUp");
    3.             if (SodaPrefab != null) Debug.Log("Prefab Is Found!");
    4.             GameObject SodaInScene = (GameObject)Instantiate(SodaPrefab);
    5.  
     
  11. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Is SodaInScene null after that is executed?
     
  12. Atix07

    Atix07

    Joined:
    Jul 27, 2013
    Posts:
    182
    @StarManta Both of them is not null... but its not cloning in the scene?
     
  13. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    That's.... odd. You're certain it's nowhere in the scene? It hasn't just been instantiated in a weird position?
     
  14. Atix07

    Atix07

    Joined:
    Jul 27, 2013
    Posts:
    182
    @StarManta OMG! LoL its teleporting to weird place omg! Haha! How I can change its position?
     
  15. Atix07

    Atix07

    Joined:
    Jul 27, 2013
    Posts:
    182
    Okay i did thanks for everythink bro!

    Code (csharp):
    1.  
    2. GameObject go = Instantiate(Resources.Load("SodaCanPickUp"),Player.position,Player.rotation) as GameObject;
    3.