instantiate problem

I got a script to instantiate an object on a certain position, but i got to problems, first it instantiates not 1 but thousands of objects, and i got a code in it to check a position and at that position the object has to be instantiated, can’t get that to work either…
Here’s the script:

var currentObject : Transform;

var idle : Transform;
var walk : Transform;
var run : Transform;
var jump : Transform;
var fight : Transform;

var currentPosition = transform.TransformPoint(Vector3.zero); 

function Update () 
{
	Instantiate(idle, currentPosition);
}
var myObject ; GameObject;
function Update () 
{
	if(!myObject ) myObject=Instantiate(idle, Vector3(0, 0, 0), Quaternion.identity);
}

Sorry edited my post, some more is going on xD

well for starters you need to understand why it is making thousands of instances.

your using the update function, so anything within that function is going to be called every frame, so right now every frame your instancing your object

im not sure what your ultimate goal of your script is so its hard to help further then that, you have a lot of variables that arnt being used, are you planning on switching a mesh with hitting a button?

if you want it to always start the game with your “idle” transform. use a Start function, this should call it only once as soon as the game starts

function Start ()
{
//do stuff
}

Okay thanks, any idea how to fix the other problem xD Seems simple but i don’t know how to implement it

your current position var might need to be in the update function so it picks up where it is every frame, idk tho,

also if you look at bigmisters post youll see his instantiate has 3 parts ,and yours only has 2.

it needs to be formatted like this

	Instantiate(  object to be made,   position to be made,   rotation of object);

your not setting any rotation

are you getting an error? what is it saying

okay uhm sorry to ask but how do i check the rotation then

Right now it gives me this error:

Assets/ReplaceObject.js(16,27): BCE0005: Unknown identifier: ‘currentPosition’.

btw here’s the current version of the script:

var currentObject : Transform;

var idle : Transform;
var walk : Transform;
var run : Transform;
var jump : Transform;
var fight : Transform;

function Update ()
{
	var currentPosition;
	currentPosition  = transform.TransformPoint(Vector3.zero); 
}
function Start () 
{
	Instantiate(idle, currentPosition);
}
var idle : Transform


function Start () 
{
	Instantiate(idle, transform.position,  Quaternion.identity);
}

that will instance your idle object when the game first starts at the location of the object with this script on it at the same rotation, lol is that what your trying to do?

Hello there!

Well this code looks a bit off to me. I’m a C# coder myself, but I’m fairly certain it should look something like this:

function Start()
{
    Instantiate(idle, transform.position, transform.rotation);
}

currentPosition is indeed an unknown variable since you declare it localy in your Update function.
Furthermore Start() is called when the object is first activated, hence it will be called before Update.

yea but if i use update it instantiate more than one object… Besides that, if i use transform.position, then how do i call the currentPosition to use that one?

Hmmmm… I think you are totally confused about animations. Acutally a subject that I dont know that much about yet. I do know however that animations are set into an Animation object not transforms. Checking the documentation it looks like that animations play almost like audio clips.

so the problem I can see is the obvious… you don’t use Instantiate to generate animations… :wink:

var anims : AnimationClip[];
private var lastClip="";

function Update(){
	var anim="idle";

	if(Input.GetKey("w")){
		//doMovement(forwardSpeed);
		anim="walk";
		animation.Play("walk", PlayMode.StopAll);
	}
	
	if(Input.GetKey("s")){
		//doMovement(backwardSpeed);
		anim="walkBackward";
	}
	
	if(anim != lastClip){
		animation.Play(anim, PlayMode.StopAll);
	}
	lastClip = anim;
}

no i don’t want to use animations i intentionelly (don’t know if that’s spelled correctly) use objects

it should always spawn at the location of the transform with the script on it. if you are spawning something that can be moved by the user, dont physically move the instanced object, but move the proxy that you have this script attached to.

also big Mister is pretty much correct, your going about it kinda the wrong way, the proper way is to use animations,

like i said originally its hard to identify what your issue is, i think we need more information about your end goal

Then you specifically want to do object replacement on the fly, so you still would not instantiate anything :wink:

var idle : Transform;
var walk : Transform;
var run : Transform;
var jump : Transform;
var fight : Transform;
private var lastClip : Transform = null;

function Start () 
{
	idle.renderer.enabled=true;
	walk.renderer.enabled=false;
	run.renderer.enabled=false;
	jump.renderer.enabled=false;
	fight.renderer.enabled=false;
}

function Update ()
{
	var anim=idle;

	if(Input.GetKey("w")){
		//doMovement(forwardSpeed);
		anim=idle;
	}
	
	if(Input.GetKey("s")){
		//doMovement(backwardSpeed);
		anim=walk;
	}
	
	if(anim != lastClip){
		lastClip.renderer.enabled=false;
		anim.renderer.enabled=false;
	}
	lastClip = anim;
}

Again, you probably wont achieve animation in any real sense with instantiate.

okay i know i use kind of an akward method, but it’s my method, but i did fix it now, thanks for that:) last problem though, how can i instantiate it as a child of a gameobject??

var obj = Instantiate(idle, transform.position, transform.rotation);
obj.transform.parent=transform; //parent it to this object

Thanks a lot, sorry for the big fuzz about all of this, but now i can do the rest myself :slight_smile: this was a little obstacle for me xD
I’ll explain what my plan was with this, as you already saw, it is meant for a character, but since i couldn’t get animations to work properly (probably doing something very wrong), i wanted to create my own character controller and instead of animations, i wanted to replace an object by another one with the correct animation attached so that it would be simple to reuse the script, and simple to make the prefabs for it. so it’s not a correct method, but it works for me :slight_smile:

Okay so there’s one last problem xD what i want to add (and did add) is that when a button is pressed the instantiation is destroyed and another object is instantiated instead, i now have this but it doesn’t work.

var currentObject : Transform;

var idle : Transform;
var walk : Transform;
var run : Transform;
var jump : Transform;
var fight : Transform;

function Update ()
{
	var currentPosition;
	currentPosition  = transform.TransformPoint(Vector3.zero); 
}
function Start () 
{
	var obj = Instantiate(idle, transform.position, transform.rotation);
	obj.transform.parent=transform;
}

if (Input.GetButton("Jump")) 
{
	Destroy(idle.gameObject);
	var obj = Instantiate(jump, transform.position, transform.rotation);
	obj.transform.parent=transform;
}

EDIT: Just found out that the problem lies in the GetButton part, but i don’t see why it’s not working…