Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Alex 8 · Dec 11, 2011 at 01:41 AM · prefabsruntimeswitch

Changing Prefabs at Run time

Hi, im currently trying to work on changing a prefab on a gun at runtime. The prefab is the bullet that is instantiated when the user presses an input, however i am looking to change the bullet when getting a certain score in the game. e.g.

if (score <= 0){

(KEEP ORIGINAL PREFAB)

 }

if (score <= 15){

(CHANGE TO NEW PREFAB - Enable/Disable statement?)

 }

i apologise for the lack of coding i have shown for this, but i am just starting on the code and was wondering if changing Prefabs at run time is even possible, as i have seen mesh and material is changeable, i am going to look into enable/disable commands as it seems the best place to start!

cheers

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Lo0NuhtiK · Dec 12, 2011 at 10:41 PM

This script is pretty basic, but it should get you started. I didn't include anything about how to increment your score, or how to send your bullet flying or anything like that. This is just a way to change your prefab like you asked about.

This assumes you're using an object with a rigidbody for your projectiles. Create an array that you can set the size of (however many different types of projectiles you want) and then drag/drop your projectiles into it from the editor->


public var projectile : Rigidbody[] ;


Then you'll have your player score variable, however you contorl its value is some other function you'll need to make->


@System.NonSerialized //<-this just keeps the playerScore out of the inspector panel
var playerScore : int = 0 ;


Now the Update function for when you call your firing function (Which in this case will be "Shoot()" ). You can change the button or key etc to whatever you want, but I'm just going with the default fire button which is the left control or left mouse. Input.GetButtonDown() only fires once each time the button is pressed.->


function Update(){
   if(Input.GetButtonDown("Fire1")) {
      Shoot() ;
   }
}

And then we go to the Shoot() function. This example assumes you're using three different projectiles that you've drag/dropped into the array in the editor. You can easily modify it to different score values for the changes, or add/delete "else if" statements for more/less projectiles in your array. This could probably be done better with switch's or for's etc, but this was a quick script. ...Anyway, here's Shoot() ->

function Shoot(){
   var clone : Rigidbody ;
      if (playerScore >= 50 ) { //if score is 50 or more
         clone = Instantiate(projectile[2], transform.position, transform.rotation) ;
      }
      else if(playerScore >= 15 && playerScore <= 49){ //if score is between 15 and 49
      clone = Instantiate(projectile[1], transform.position, transform.rotation) ;
      }
      else{ //if score is anything other than the above use default/beginner projectile
      clone = Instantiate(projectile[0], transform.position, transform.rotation) ;
      }
}

All of the above goes into one script, which would be attached to whatever object you're instantiating at.
ie: an empty object placed in the front-center of your gun barrel.
This will instantiate your prefab(s) at that objects location, with said objects rotation. Hope this helps.
Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Saving Runtime Generated GameObjects to Prefabs 2 Answers

How can record the total amount of prefabs that are spawned during runtime? 0 Answers

Where is Actually The Prefabs Stored Before Instantiation? RAM or Storage(Internal Memory) 2 Answers

Create prefab from gameobjects at runtime 2 Answers

change various Prefabs (primitives) with a Gui.Button // switching Prefabs 2 Answers


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