- Home /
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.
$$anonymous$$indly do not ask duplicated questions: http://answers.unity3d.com/questions/17914/how-to-make-a-customizable-item
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.
@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!
"$$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.
@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!
Did you assign the correct prefab(s) to the cube(s) in the project explorer?
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!
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613071139im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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