- Home /
No Overload for method 'Instantiate' takes '7' arguments?
Hello, First of all I want to apoligize if this is a stupid question.
I am fairly new to unity and C# in general but I was (almost) able to get together a health script, My issue is respawning, I am attempting to use Instantiate to clone the player and respawn him at the spawn point, here is that bit of code
Instantiate("Player", 23.21657, 3.257863, -0.6684538,0, 0, 0);
But when I go to build it I get the error in the title, I already have the 7 args though?
Anyway, here is the full code, Thanks in advance for any help
enter code here
public class Health : MonoBehaviour {
public float maxHealth = 100.0f ;
public float curHealth = 100f ;
void Start(){
curHealth = maxHealth ;
var Player = gameObject.CompareTag("Player");
}
void Update(){
bool Player = gameObject.CompareTag("Player");
if(curHealth < 0)
curHealth = 0;
if(curHealth == 0)
Instantiate("Player", 23.21657, 3.257863, -0.6684538,0, 0, 0);
if(curHealth > maxHealth)
curHealth = maxHealth ;
}
void OnGUI(){
GUI.Label(new Rect(100,100,100,100), curHealth + "/" + maxHealth);
}
void ApplyDamage(float damage){
curHealth -= damage ;
}
void OnTriggerEnter(Collider other) {
if(other.gameObject.CompareTag("Zombie")){
ApplyDamage(10);
}
}}
The error is saying that there are no versions of Instantiate that can take 7 arguments. There is only one version of Instantiate, and it takes only 3 arguments: an original Object, a position, and an orientation.
Instantiating is used for cloning an object. You can't clone the player from just the name alone. You have to use the original, but I don't believe that works if you have destroyed the original.
Ins$$anonymous$$d, try looking into prefabs. If you create a prefab out of the player object and then create a new instance of the prefab, this will give you the effect you want.
thanks this helps me in something I am working on today
Answer by Seizure · Jul 30, 2013 at 07:27 PM
Just drop your player prefab on Player in Unity Editor and use this code:
using UnityEngine;
using System.Collections;
public class Health : MonoBehaviour
{
public float maxHealth = 100.0f ;
public float curHealth = 100f ;
public GameObject Player;
void Start(){
curHealth = maxHealth ;
}
void Update(){
if(curHealth < 0)
curHealth = 0;
if(curHealth == 0)
Instantiate(Player, new Vector3 (23.21657f, 3.257863f, -0.6684538f), Quaternion.identity);
if(curHealth > maxHealth)
curHealth = maxHealth ;
}
void OnGUI(){
GUI.Label(new Rect(100,100,100,100), curHealth + "/" + maxHealth);
}
void ApplyDamage(float damage){
curHealth -= damage ;
}
void OnTriggerEnter(Collider other) {
if(other.gameObject.CompareTag("Zombie")){
ApplyDamage(10);
}
}
}
Answer by sdadadwqR · Jun 02, 2015 at 10:49 PM
zxzxzxzxzxxz poo is nutritious
add pampamsam on ROBLOX
Answer by jacobschellenberg · Jul 30, 2013 at 07:23 PM
Instantiate takes a GameObject, Vector3 Position and Rotation.
For example:
public GameObject playerPrefab;
void Start(){
Instantiate(playerPrefab, new Vector3(0, 0, 0), Quaternion.Identity);
}
Don't forget to assign the prefab to the public variable in the Inspector.
So essentially you could end up with this:
using UnityEngine;
using System.Collections;
public class Health : MonoBehaviour
{
public float maxHealth = 100.0f ;
public float curHealth = 100f ;
public GameObject playerPrefab;
void Start(){
curHealth = maxHealth ;
}
void Update(){
if(curHealth < 0)
curHealth = 0;
if(curHealth == 0)
Instantiate(playerPrefab, new Vector3 (23.21657f, 3.257863f, -0.6684538f), Quaternion.identity);
if(curHealth > maxHealth)
curHealth = maxHealth ;
}
void OnGUI(){
GUI.Label(new Rect(100,100,100,100), curHealth + "/" + maxHealth);
}
void ApplyDamage(float damage){
curHealth -= damage ;
}
void OnTriggerEnter(Collider other) {
if(other.gameObject.CompareTag("Zombie")){
ApplyDamage(10);
}
}
}
Hope this helps :)
Answer by Bunny83 · Jul 30, 2013 at 07:30 PM
Well, pretty much what the error is telling you. There are multiple versions of Instantiate but non takes 7 arguments, only 3. You want something like:
Instantiate(player, new Vector3(23.21657f, 3.257863f, -0.6684538f), Quaternion.Euler(0f,0f,0f));
where player should be a reference to a prefab. You can't just use a string and expect Unity to know what you mean by this.
Actually your whole script doesn't make much sense. The CompareTag line in Start is totally useless since it does nothing.
Calling Instantiate every frame as long as curHealth is 0 doesn't make much sense to me. This would instantiate hundreds of objects within seconds. If you really want to recreate the player object you should destroy the old one afterwards. However that usually caused even more problems. It's usually way simpler to just "reset" your players health and move the player to the respawn position.
Also you should follow at least one common indent style because your code is formatted in a very confusing matter ;)
Your answer
Follow this Question
Related Questions
Future Augment 1 Answer
Cloud recognition in Vuforia 0 Answers
Program control not entering onTriggerEnter() 1 Answer
why I am not getting highest value in my highscore textfield? 1 Answer