I want to be able to specify which prefab among a number of weapon prefabs I want to be instantiated, i.e. if myGun int is 1, then string weaponToInstantiate is “WeaponOne”, and then later in the code, Instaniate(weaponToInstantiate). When I do this I get an error:
error CS1502: The best overloaded method match for `UnityEngine.Object.Instantiate(UnityEngine.Object)’ has some invalid arguments
and
error CS1503: Argument #1' cannot convert string’ expression to type `UnityEngine.Object’
Does anyone know how to go about this? (Note: all weapon prefabs are tagged as CurrentGun, and I did not specify position or rotation, because I want the default position and rotation of each weapon to be used)
5 Answers
5
Define a Gun Factory class that has a list of all the Gun GameObjects
Using System.Collections.Generic;
public class GunsFactory : MonoBehavior
{
public List<GameObject> guns = new List<GameObject>();
public GameObject CreateGun(int gunIndex)
{
return Instantiate(guns[gunIndex]);
}
}
You will then create attach it to a game object and add all of your gun prefabs to it.
If you want to reference them by strings instead, you could use a dictionary:
Dictionary<String, GameObject> guns;
You probably want to make it a singleton so you can easily access it from anywhere, but that is a topic for another discussion.
You can put your prefabs in a folder called “Resources”, and then load the prefab by its name using Resources.Load:
public void CreateGun(string gunName) {
GameObject gunPrefab = Resources.Load<GameObject>(gunName);
Instantiate(gunPrefab);
}
Make sure you create prefabs: Create a “Resources” folder in your assets. It must be named “Resources”. Create a prefab for each gun you want by dragging the gun from the scene to the Resources folder in the project (See here about creating prefabs). Make sure the name of the prefab in the resources folder is the same as the name you pass to the CreateGun() method.
if the other method does not work for you then you can reference this code from one of my projects. just change the if statement and the specific names. I hope that this helped
I apologize for the bad formatting of the code I have never posted code in response to a question.
if (Input.GetKeyDown(KeyCode.Space)) {//fire lazer Vector3 position = new Vector3(myTransform.position.x, myTransform.position.y, myTransform.position.z); Instantiate(ProjectileFab, position,Quaternion.identity);}
1.) Create a prefab in the editor and place into the Assets/Resources folder. The Resources folder is a special folder that allows you to load assets at runtime.
2.) Load the prefab using the following code…
GameObject gunPrefab = Resources.Load("yourgunPrefabName") as GameObject;
if(gunPrefab != null)
GameObject.Instantiate(gunPrefab, Vector3.zero, Quaternion.Identity);
Depending how many guns you are going to have for the final version of your game some of these methods may be a little overkill especially if you are new to programming. All the list, dictionary, and resources stuff could be very useful but you should save that for once you are more comfortable with this stuff.
For starting a system like this I would probably do something like:
//do a Transform for the guns position and rotation when you instantiate
//this should probably be a child gameObject on your player
public Transform gunTransform;
//declare a few Game Objects at beginning of script
//each of these should be a public variable
//you can easily drag your prefabs from assets into public Game Objects in script
public GameObject gun0Object;
public GameObject gun1Object;
public GameObject gun2Object;
public GameObject gun3Object;
//create a public static int
//allows easy access to what gun is being used
//doesn't need to be public unless your other scripts should access it super easily
//for example your enemies could know how much damage to take when shot
//by checking what kind of gun you are using based on this identifier int
public static int currentGunInt;
//this system allows you to always know which gun you used last
void Start()
{
//check which gun to start with a PlayerPref
//the 0 is whatever gun you start with or being unarmed
//will only be 0 if you haven't saved anything else to this PlayerPref
//or that's the last gun you used
currentGunInt = PlayerPrefs.GetInt ("Gun Int", 0);
//now that you know which gun to use you call a function to instantiate prefab
//you can use this function at start as well as during gameplay when you switch guns
void equipGunFunction();
}
void equipGunFunction()
{
if (currentGunInt == 0)
{
PlayerPrefs.SetInt ("Gun Int", 0);
Instantiate (gun0Object, gunTransform.position, gunTransform.rotation) as GameObject;
}
else if (currentGunInt == 1)
{
PlayerPrefs.SetInt ("Gun Int", 1);
Instantiate (gun1Object, gunTransform.position, gunTransform.rotation) as GameObject;
}
//etc for all the guns
}
I think that’s the easiest way to do it and you shouldn’t be getting any errors keeping it simple. Hopefully I’m not just repeating what others have posted already, I didn’t read everything in great detail just wanted to bust out a quick example hope that helps!
Edit: Oh and you’ll want to add some more variables once you get this working if you use this. So you could have another public GameObject, PlayerPref, and public static int for the previous gun used so you could also destroy the old prefab in the same function where you create the new one. Or it could be used to toggle your 2 guns.
Why not use variables? var Wep : GameObject; function Update(){ Instantiate(Wep, blah, blah); }
– applejuices