x


How can i assign prefab to variable without drag & drop from project to inspector.

example: I have a script that instantiate a weapon.

function SetWeapon(weaponType:GameObject){
     var clone = Instantiate (weaponType, transform.position, transform.rotation);
}

How can i pass prefab in SetWeapon(); or if i have something like this:

var weapon:GameObject;

function SetWeapon(){
         var clone = Instantiate (weapon, transform.position, transform.rotation);
    }

I know how to assign prefab from inspector, but how to change it from runtime. And if it is possible I like to avoid my weapons siting in the scene and setting weapon variable by gameObject.Find();

Thanks in advance :P

more ▼

asked Oct 12 '10 at 08:22 AM

beloto gravatar image

beloto
96 6 6 15

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You can do this by making use of the Resources Folder.

From the example on the Resources.Load scripting reference:

// Instantiates a prefab at the path "Assets/Resources/enemy".

function Start () {
    var instance : GameObject = Instantiate(Resources.Load("enemy"));
}

However

Why do you want to do this?

It might be a good idea if you stated your broader goal - why you want to avoid prefab references - because it might help someone give a more helpful answer.

There are many reasons why prefab references are a good way to organise your project, and trying to bypass the way Unity is designed to work is generally not a good idea unless you have a specific reason that warrants it.

more ▼

answered Oct 12 '10 at 08:37 AM

duck gravatar image

duck ♦♦
41k 92 148 415

Thank you for the answer. I was thinking to call SetWeapon() with the name of the prefab as a parameter and then menage the destruction of the previous weapon and instantiating of the new one in the body of the function. That way i can swap the weapon with a call of single function. And when i think about it again. I can do the same by assigning the same script with different script links in the inspector on the buttons that change the weapon. Resources.Load solve the problem the way I imagine it should be solved.And i am not sure that is the best way hehehe thank you again ;)

Oct 12 '10 at 09:49 AM beloto

Yes, this isn't the best way. What you describe doesn't warrant the use of "Resources". You could use a Hashtable if you specifically want to reference your weapons by name, but typically this isn't necessary. I'd suggest using a "weapons" array, in which you can place a reference to every kind of weapon prefab, and then pulling out items from that array for instantiation.

Oct 12 '10 at 11:48 AM duck ♦♦

Ah yeah array. that will be great way to house all the prefabs in one variable - place. and then i can pass separate index in the http://function.It will do exactly what i need. I already make solution without Resources.Load() where i put the same script on a different skill button, but linked different prefabs. In that way I reused the same script. Thank you for the array idea, that will work great also.

Oct 12 '10 at 02:46 PM beloto
(comments are locked)
10|3000 characters needed characters left
//just drop this script in button that you want to act like weapon changer
//use this var in inspector to set the parent of the weapon (drag and drop)
var weaponParent:GameObject;
//use this var in the inspector to set the weapon to be instanced
var weaponToSet:GameObject;
//set this one in the inspector for the keyboard shortcut (default 0)
var shortcut:String="0";

//tag weapons with weapon tag to get cleaned by this function
function RemoveOldWeapon (){
    var weapons : GameObject[];
    weapons = GameObject.FindGameObjectsWithTag("weapon"); 
    if(weapons.length > 0){
        for (var wep: GameObject in weapons)  { 
            Destroy(wep);
        } 
    }
}

//function that instantiate the weapon
function SetWeapon (){
    var clone = Instantiate (weaponToSet, weaponParent.transform.position, weaponParent.transform.rotation);
    clone.transform.parent = weaponParent.transform;
}

//function that use mouse klick to swap the weapon
function OnMouseDown (){
    RemoveOldWeapon();
    SetWeapon();
}

function Update (){
    //function that use keyboard shortcut to swap the weapon
    if (Input.GetKeyDown (shortcut)){
        RemoveOldWeapon();
        SetWeapon();
    }

}

here is the solution that i make when i rethink the problem. Big thanks to Duck for helping me come with this solution.

more ▼

answered Oct 12 '10 at 03:09 PM

beloto gravatar image

beloto
96 6 6 15

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1678
x1259
x824
x467
x5

asked: Oct 12 '10 at 08:22 AM

Seen: 3870 times

Last Updated: Oct 12 '10 at 08:22 AM