Infinite prefab spawn bug - unity3d

I have a simple spawn script for my 2D game that i wrote, that i want to spawn an object after a specific period of time. I managed to get this to work but the one problem is that the object keeps spawning. I just want the object to spawn once not an infinite amount.
var myTimer : float = 5.0;
var thePrefab : GameObject;
function Update () {
if(myTimer > 0){
myTimer -= Time.deltaTime;
}
if(myTimer <= 0){
var instance : GameObject = Instantiate(thePrefab, transform.position, transform.rotation);
}
}

By shifting around your if statements, you can restrict your object to only spawning once:
var myTimer : float = 5.0;
var thePrefab : GameObject;
function Update () {
if(myTimer > 0){
myTimer -= Time.deltaTime;
if(myTimer <= 0){
var instance : GameObject = Instantiate(thePrefab, transform.position, transform.rotation);
}
}
}
Now, the object will only spawn if myTimer > 0 prior to the decrement, and myTimer <= 0 following the decrement - which only happens once.
Hope this helps! Let me know if you have any questions.

Related

Unity Javascript transform object not able to go in

var Distance;
var Target = transform;
var lookAtDistance = 25.0;
var attackRange = 15.0;
var moveSpeed = 5.0;
var Damping = 6.0;
function Update ()
{
Distance = Vector3.Distance(Target.position, transform.position);
if (Distance < lookAtDistance)
{
lookAt();
}
if (Distance > lookAtDistance)
{
}
if (Distance < attackRange)
{
attack ();
}
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(Target.position -
transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation,
Time.deltaTime * Damping);
}
function attack ()
{
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
}
I am trying to make my object go towards the player but i cant put anything in the slot where it prompts you to put the object i want it to follow
the problem i believe is at line 2 but i have tried so many things and i cant do anything to fix it
i would like to note i have another piece of code simialr to this in a different script and it works just fine.
Replace var Target = transform; with var Target:Transform; on line 2.

Click and Hold OnMouseDown Unity3D

I want my OnMouseDown command to work only when the player presses over an object within the game. At the moment, the player can press anywhere and the timer will start. Is there a way to have the timer only start when they click and hold an object? To give you some context, I want an object to become inactive when the player clicks and holds over it.
Here is my code so far:
function Update ()
{
if (Input.GetMouseButton(0))
{
timer += Time.deltaTime;
}
if ( Input.GetMouseButtonUp(0))
{
timer = 0;
}
if(timer > 1)
{
gameObject.SetActive(false);
}
}
SOLUTION:
I used a Raycast in order to do this:
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if (Input.GetMouseButton(0) && coll.Raycast(ray,hit,100))
{
timer += Time.deltaTime;
}
if ( Input.GetMouseButtonUp(0))
{
timer = 0;
}

Why my Players life disappear at once?

I am new in unity and I am developing game like Flippy Bird where if my player is collide with another object then one life of my player will disable and the collider is travel from one place to another place and there is so many colliders and the collider is little bit wide so when my player touch with that collider then its three life is disabled one by one in very short time.
I understand that when my player touch the same object again and again. So that players all life cut at the same time but i don't know how to solve this.
Code for Collision is below.
void OnTriggerEnter(Collider C){
if(C.gameObject.name == "DownCollider" || C.gameObject.name == "Cylinder"){
if(Life1.activeInHierarchy){
Life1.SetActive(false);
}
else if(Life2.activeInHierarchy){
Life2.SetActive(false);
}
else if(Life3.activeInHierarchy){
Life3.SetActive(false);
}
}
I just try this and it works for me..
void Update () {
Vector3 pos = transform.position;
pos.y -= 0.03f;
transform.localRotation = Quaternion.Euler(60,270,90);
transform.position = pos;
if(Input.GetMouseButton(0)){
transform.localRotation = Quaternion.Euler(72,90,-90);
pos.y += 0.1f;
transform.position = pos;
}
LifeT -= Time.deltaTime;
if(LifeT <= 0){
LifeTime = true;
LifeT = 5f;
}
}
void OnTriggerEnter(Collider C){
if(C.gameObject.name == "DownCollider" || C.gameObject.name == "Cylinder"){
print(LifeTime);
if(Life1.activeInHierarchy && LifeTime){
Life1.SetActive(false);
LifeTime = false;
}
else if(Life2.activeInHierarchy && LifeTime){
Life2.SetActive(false);
LifeTime = false;
}
else if(Life3.activeInHierarchy && LifeTime){
Life3.SetActive(false);
LifeTime = false;
}
}
Thanks for help KennethLJJ.
Regards,
Dharmesh

How to Put this strength meter bar to an object?

#pragma strict
var strengthAmount : float = 0.0F;
private var maxStrengthAmount : float = 100.0F;
var guiSkin : GUISkin;
var barSprite : Texture2D;
var powerButton : GUITexture;
function Update () {
for (var touch : Touch in Input.touches)
{
if (strengthAmount >= 100.0F)
{
strengthAmount = maxStrengthAmount;
// Do stuff here
}
else if (strengthAmount < 0.0F)
{
strengthAmount = 0.0F;
}
if(touch.phase == TouchPhase.Stationary && powerButton.HitTest(touch.position)){
strengthAmount += 1.0F;
}
else if(touch.phase == TouchPhase.Ended && powerButton.HitTest){
strengthAmount = 0.0F;
}
}
}
function OnGUI()
{
GUI.skin = this.guiSkin;
GUI.DrawTexture(new Rect(Screen.width / 8, Screen.height - 167.5F, 150
(strengthAmount / maxStrengthAmount), 31), barSprite);
GUI.Box(new Rect(Screen.width / 8, Screen.height - 167.5F, 150, 31), "Strength");
}
Here is my strength meter bar code, but I want to put it on an object that when I release the button, the object will be thrown away? I am new to Unity3D so please help me how to make a script on that or help me directly thank you
Create a JS script, paste this script and save it. Create a game object, select it, on the inspector click on the button Add component. Then find your script and add it.
Then create another script that has the following:
Update() //this is C#
{
if (GetKeyUp(KeyCode. /*here find the key code of the button that you want to release*/ )
Destroy(this); //if you want to destroy the object
}

How to get projectile to move in the direction the player is facing

This is the code for my player's movement:
var animator : Animator;
function Start () {
animator = GetComponent("Animator");
}
var moveUp : KeyCode;
var moveDown : KeyCode;
var moveLeft : KeyCode;
var moveRight : KeyCode;
var jumpheight : int = 2;
var speed : int = 2;
var isGrounded : int = 0;
// Checks if player is in air or ground
var Jumptest : int = 0;
// Checks if player has jumped
function Update () {
animator.SetInteger("Direction", 4);
if (Input.GetKey(moveUp) && isGrounded == 0)
{
rigidbody2D.velocity.y = jumpheight;
isGrounded = 1;
animator.SetInteger("Direction", 2);
}
else if (Input.GetKey(moveLeft))
{
transform.position += Vector3.left * speed * Time.deltaTime;
animator.SetInteger("Direction", 3);
}
else if (Input.GetKey(moveRight))
{
transform.position += Vector3.right * speed * Time.deltaTime;
animator.SetInteger("Direction", 1);
}
}
and this is the code for the projectiles movement:
#pragma strict
var speed = 8.0;
var time = 0.0;
function Start () {
}
var moveRight : KeyCode;
var moveLeft : KeyCode;
function Update () {
time += Time.deltaTime;
if(time > 3){
Destroy(gameObject);
}
transform.Translate(Vector3.right * speed * Time.deltaTime, Space.Self);
}
At the moment, the projectiles shoot to the right. I want to be able to shoot them in the direction the player is facing.
I tried if key down a and if key down d but then the projectiles stop when the player is not moving and move relative to the player (if I shoot one going left, then move my player to the right, the projectile reverses direction).
Anybody know?
When checking for player movement , whether the left/right key is down, you create a simple integer which keeps track of direction
...
var isRight: int=1 ;//assuming player starts with facing in the right direction
...
and then, when checking for key press set the value of this variable appropriately
...
else if (Input.GetKey(moveLeft)&&!Input.GetKey(moveRight))
{
isRight = -1;
...
}
else if (Input.GetKey(moveRight)&&!Input.GetKey(moveLeft))
{
isRight = 1;
...
}
Finally when setting translation for the projectile simply multiply the speed with this new variable
transform.Translate(Vector3.right * speed * isRight * Time.deltaTime, Space.Self);