How to put prefab without dragging it?

Hello! So I got some problems about this. I made this code but ıts not working. Where is the problem ? Thanks!

GameObject SodaPrefab = (GameObject)Resources.Load("/SodaCanPickUp");

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 /.

GameObject SodaPrefab = (GameObject)Resources.Load("SodaCanPickUp");
GameObject SodaInScene = (GameObject)Instantiate(SodaPrefab);

@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:

using UnityEngine;
using System.Collections;

public class Soda : MonoBehaviour
{
    private vp_FPInventory m_Inventory = null;
    private PlayerAyar m_PlayerAyar = null;

    public Transform Player;

    GameObject SodaPrefab = (GameObject)Resources.Load("/SodaCanPickUp");

    // Use this for initialization
    void Awake ()
    {
        Player = GameObject.FindGameObjectWithTag("Player").transform;
        m_Inventory = Player.transform.GetComponent<vp_FPInventory>();
        m_PlayerAyar = Player.transform.GetComponent<PlayerAyar>();
    }
  
    // Update is called once per frame
    void Update ()
    {
        vp_ItemInstance currentObject = m_Inventory.CurrentWeaponInstance as vp_ItemInstance;
      
        if (Input.GetMouseButtonDown(0))
        {
            m_PlayerAyar.Susuzluk = m_PlayerAyar.Susuzluk - 20;
            m_Inventory.TryRemoveItem(currentObject);
        }
        else if (Input.GetKeyDown(KeyCode.G))
        {
            Rigidbody clone;
            clone = Instantiate(SodaPrefab, transform.position, transform.rotation) as Rigidbody;
            clone.velocity = transform.TransformDirection(Vector3.forward * 10);
        }
    }
}

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.

GameObject SodaPrefab

void Reset() {
SodaPrefab = (GameObject)Resources.Load("SodaCanPickUp");
}

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.

@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?

I don’t understand your question?

@StarManta I got Eror. Its says: The thing you want to instantiate is null.

GameObject SodaPrefab = (GameObject)Resources.Load("SodaCanPickUp");

Why I cant find the prefab with this object? It says ıts null.

Is there an object of that name in the root Resources folder? (Screenshot?)

Here is the screenshot. @StarManta

1710255--107650--Untitled-1.jpg

@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.

GameObject SodaPrefab = (GameObject)Resources.Load("SodaCanPickUp");
            if (SodaPrefab != null) Debug.Log("Prefab Is Found!");
            GameObject SodaInScene = (GameObject)Instantiate(SodaPrefab);

Is SodaInScene null after that is executed?

@StarManta Both of them is not null… but its not cloning in the scene?

That’s… odd. You’re certain it’s nowhere in the scene? It hasn’t just been instantiated in a weird position?

@StarManta OMG! LoL its teleporting to weird place omg! Haha! How I can change its position?

Okay i did thanks for everythink bro!

GameObject go = Instantiate(Resources.Load("SodaCanPickUp"),Player.position,Player.rotation) as GameObject;