- Home /
I keep getting that "Instantiate" problem.
I keep getting the same error, can anybody help me by explaining why the problem appears, or at least give me the proper code?:
Assets/Scripts/TurretScript.js(72,17): BCE0023: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(System.Type, UnityEngine.Vector3, UnityEngine.Quaternion)' was found.
................................................................................................................................................................................. THE CODE:
function FireProjectile() { audio.Play(); nextFireTime = Time.time+reloadTime; nextMoveTime = Time.time+firePauseTime; CalculateAimError();
for(theMuzzlePos in muzzlePositions) { Instantiate (myProjectile, theMuzzlePos.position, theMuzzlePos.rotation); Instantiate (muzzleEffect, theMuzzlePos.position, theMuzzlePos.rotation); } }
Answer by whydoidoit · Mar 31, 2013 at 10:23 AM
For one of your instantiate calls you may have a script called myProjectile or muzzleEffect and it's using that rather than a variable that contains the prefab. Generally name your scripts using a capital letter to avoid this.
Another cause of this is defining variables like this:
var somePrefab = GameObject;
When it should be like this:
var somePrefab : GameObject;
You also need to assign the prefab in the inspector (which won't work with the first example!)
Also, I get a problem, where the canon in my my game is supposed to shoot at an enemy, but it shoots the ground. Here's the whole script:
pragma strict
var myProjectile : GameObject; var reloadTime : float = 1f; var turnSpeed : float = 5f; var firePauseTime : float = .25f; var muzzleEffect : GameObject; var errorAmount : float = .001; var myTarget : Transform; var muzzlePositions : Transform[]; var turretBall : Transform;
private var nextFireTime : float; private var next$$anonymous$$oveTime : float; private var desiredRotation : Quaternion; private var aimError : float;
function Start () {
}
function Update () { if(myTarget) { if(Time.time >= next$$anonymous$$oveTime) { CalculateAimPosition(myTarget.position); turretBall.rotation = Quaternion.Lerp(turretBall.rotation, desiredRotation, Time.deltaTime*turnSpeed); } if(Time.time >= nextFireTime) { FireProjectile(); } } } function OnTriggerEnter(other : Collider) { if(other.gameObject.tag == "Enemy") { nextFireTime = Time.time+(reloadTime*0.5); myTarget = other.gameObject.transform; } } function OnTriggerExit(other : Collider) { if(other.gameObject.transform == myTarget) { myTarget = null; } } function CalculateAimPosition(targetPos : Vector3) { var aimPoint = Vector3(targetPos.x+aimError, targetPos.y+aimError,targetPos.z+aimError); desiredRotation = Quaternion.LookRotation(aimPoint); }
function CalculateAimError() { aimError = Random.Range(-errorAmount, errorAmount); }
function FireProjectile() { audio.Play(); nextFireTime = Time.time+reloadTime; next$$anonymous$$oveTime = Time.time+firePauseTime; CalculateAimError();
for(the$$anonymous$$uzzlePos in muzzlePositions) { Instantiate ( myProjectile, the$$anonymous$$uzzlePos.position, the$$anonymous$$uzzlePos.rotation); Instantiate ( muzzleEffect, the$$anonymous$$uzzlePos.position, the$$anonymous$$uzzlePos.rotation); } }
Your answer
Follow this Question
Related Questions
Checking if object intersects? 1 Answer
PhotonNetwork.Instantiate Argument Error. 1 Answer
Multiplying GUIs 0 Answers
How do you make the waypoint system work for objects instantiated? 2 Answers
Using a top level variable in a class 0 Answers