Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by beloto · Oct 12, 2010 at 08:22 AM · instantiateprefabvariableinspector

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

Answer by duck · Oct 12, 2010 at 08:37 AM

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image beloto · Oct 12, 2010 at 09:49 AM 0
Share

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 ;)

avatar image duck ♦♦ · Oct 12, 2010 at 11:48 AM 2
Share

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.

avatar image beloto · Oct 12, 2010 at 02:46 PM 0
Share

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

avatar image
0

Answer by beloto · Oct 12, 2010 at 03:09 PM

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image daviddickball · Jul 09, 2014 at 05:23 PM 0
Share

Does this mean all your weapons are available in the scene but off camera?

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Instantiate new object from scratch 1 Answer

Clone of prefabs copy original´s script state during runtime? 0 Answers

Adding behaviors to a game object at run time via scriptable object 1 Answer

What is the type of my prefab? 2 Answers

Passing a variable to the child of Instantiated object? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges