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 john 2 · Aug 13, 2010 at 01:36 PM · physicsitem

How do you make an item to use.

i would like to make a script that when i run into a certain object make weapon prefab(in this case bullet prefab) changes. (ex. i start with my fireball prefab, i run into a certain cube, my fireball prefab changes into my iceball prefab.) I know it has to deal with the function OnTriggerEnter(hit : Collider) and possibly tags, but i don't know the script to change a prefab.

Comment
Add comment · Show 1
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 Ricardo · Aug 13, 2010 at 02:14 PM 0
Share

$$anonymous$$indly do not ask duplicated questions: http://answers.unity3d.com/questions/17914/how-to-make-a-customizable-item

2 Replies

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

Answer by spiel2001 · Aug 13, 2010 at 02:44 PM

I would think you would just create a global to hold the current weapon prefab and then change that when you hit the trigger...

so your script that throws fireball or iceballs would contain something like this...

var weaponPrefab : GameObject; // drag and drop default prefab in explorer

function OnTriggerEnter( hit : Collider ) { var newPrefab : WeaponPrefabSpec = hit.gameObject.GetComponent( "WeaponPrefabSpec" );

 if( newPrefab && newPrefab.weaponType ) 
     weaponPrefab = newPrefab.weaponType;

}

then the "box" you collide with that determines what type of prefab to use would have a script titled "WeaponPrefabSpec" that just has a variable value in it...

var weaponType : GameObject;

Now you can attach the script to the cube and assign the prefab you want used when you collide with it to the "weaponType" variable. When the player controller collides with the cube, if the cube has the script and a value assigned to the variable, the player will change the prefab they're using.

Comment
Add comment · Show 5 · 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 john 2 · Aug 13, 2010 at 04:10 PM 0
Share

@spiel2001 whenever i type that code in it gives me errors: 1. weaponType is not a member of UnityEngine.gameObject. 2.$$anonymous$$ identifier $$anonymous$$yFireballPrefab.

when i type in the name of my fireball prefab(Fireball) it then says: $$anonymous$$ Identifier Fireball.

is there something special i need to do or fill in with the script? Please comment back, Thanks!

avatar image spiel2001 · Aug 13, 2010 at 04:40 PM 0
Share

"$$anonymous$$yFireballPrefab" should be renamed to whatever you named your fireball prefab object which, I presume, is a game object of some sort. You could just use "var weaponPrefab : GameObject;" without assigning a default and then drag and drop your fireball prefab onto it in the project explorer. The other was a mistake on my part... it should have been "newPrefab.weaponType" -- my bad -- I've edited the example to reflect both changes.

avatar image john 2 · Aug 13, 2010 at 05:47 PM 0
Share

@spiel2001 i have the WeaponPrefabSpec script attached to my trigger cube, and my other one to my character. I get no errors, but it still does not switch my character's bullets from fire to ice. Please comment back, Thanks!

avatar image spiel2001 · Aug 13, 2010 at 06:59 PM 0
Share

Did you assign the correct prefab(s) to the cube(s) in the project explorer?

avatar image john 2 · Aug 13, 2010 at 07:38 PM 0
Share

yes i did. it wrote, and did everything you told me to. Also is there a way to destroy the cube, because when i said Destroy(gameObject)it gives me an error that it wants a : at the end ins$$anonymous$$d of a ;. Please comment back, Thanks!

avatar image
0

Answer by Wolfram · Aug 13, 2010 at 02:41 PM

Assuming you instantiate these prefabs when you fire a bullet, the general idea would be to assign both normal bullet prefab and ice prefab in the Inspector, have the trigger script set some global variable denoting the current bullet type, and then instantiate the bullet depending on the type.

public var normalBulletPrefab:GameObject; public var iceBulletPrefab:GameObject; public var isIceBullet:bool; // you probably want to use an enum here, to have different kinds of bullets

function FireBullet(){ if(isIceBullet) Instantiate(iceBulletPrefab); else Instantiate(normalBulletPrefab); }

and in your Trigger script you would set the isIceBullet variable of this script.

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

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Calcuation Difference Between AddForce and AddForceAtPosition 1 Answer

Effective 2.5D Seeker Missiles? 1 Answer

Script not working (Java script) 2 Answers

Problem with Rigidbody Parent/Child Relationship with "Teeter Totter" 0 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