- Home /
Can someone help me with my script. It won't work for me.
Okay so the script I have here is written in C# and it is designed so that when the period key is pressed the player is warped to a random object in the game. This object is a sphere I have called medium and has a collier on it and "in trigger" is checked on each of them. It is set up with the intention of warping randomly between them in the game. When the key is pressed I get no reaction at all in the scene so and I have tried using the medium one object as the object it looks for as well as the whole prefab I have these spawn points in. So what have I done wrong? Code below: using UnityEngine; using System.Collections;
public class warp: MonoBehaviour
{
public GameObject[] obj;
private int counter;
void Start()
{
counter = 0;
}
void Update()
{
if(Input.GetKeyUp(KeyCode.Period))
{
this.transform.position = new Vector3(obj[counter].transform.position.x, obj[counter].transform.position.y, obj[counter].transform.position.z);
counter++;
if(counter >= obj.Length) counter = 0;
}
}
}
Just in case too, you added some GameObjects in your array ?
you will need something like obj = GameObject.FindObjectsWithTag("spawnPoint");
to populate your obj[] array with positions, as it stands now, all of the positions are the same, so while it may be working, you wont notice anything at all. incidentally, this may not be random as you are incrementing the counter
variable by a set condition. you might want to use Random.Range($$anonymous$$,max)
ins$$anonymous$$d
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html
@Bubzy: The positions are not all the same. They depend on the array that is passed in "obj". Also, as it stands, he "circulates" over all the points one-by-one, rather than jumping purely randomly (as you pointed out). I am not sure if pure randomly is something he wants.
@Dr.Z and @Sajidfarooq I'm sorry but I'm sort of new and don't quite know what you mean with either of these. Sorry can you please explain or help with it a little bit please.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Help with movement script 2 Answers
Vehicle help 1 Answer
how do i use two animations on the same object?? 1 Answer
Activating Audio When Hitting a collider 2 Answers