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);
}
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
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?
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…
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;
}
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
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??
Thanks a lot, sorry for the big fuzz about all of this, but now i can do the rest myself 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
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…