Delayed Set.Active

I am trying to make it so that an object appears after 5 seconds in c#. I have tried nearly everything and I cannot figure it out.

using UnityEngine;
using System.Collections;

public class Spawn_delayer : MonoBehaviour {

    public GameObject north_wall;

    void Foo ()
    {
        StartCoroutine (Begin ());
    }
    IEnumerator Begin ()
    {
        yield return new WaitForSeconds (3);
        transform.position = new Vector3 (104, 0, 76);
        print (transform.position.x);
    }
    // Use this for initialization
    void Start ()
    {
    }
    // Update is called once per frame
    void Update ()
    {
    }
}

this is the code i have but it doesn’t seem to work, please help.

First, what gameobject are you trying to move with this script?
The north_wall, or the gameobject with this script attached to it?

Also, you are not calling the function Foo anywhere, so this might account for nothing happening.

Also, please use CODE tags so that your code is actually readable here on the forums.

I’m trying to move the north_wall thats the object the game object the script is attached to

sorry just fixed it

I tested your script in an empty project of mine, and it works just fine, so long as you call the Foo function somewhere.

1 Like

Really?! thanks im gonna try it now

Where did you call the Foo function to get it to work

I put it in the Start Function.

1 Like

It still isnt working for me could you show me your code? Also thank you so much for all of your help

Yup, no problem.

using UnityEngine;
using System.Collections;

public class WaitingTest : MonoBehaviour {
    public GameObject north_wall;
  
    void Foo ()
    {
        StartCoroutine (Begin ());
    }
    IEnumerator Begin ()
    {
        yield return new WaitForSeconds (3);
        transform.position = new Vector3 (104, 0, 76);
        print (transform.position.x);
    }
    // Use this for initialization
    void Start ()
    {
        Foo();
    }

    // Update is called once per frame
    void Update ()
    {
    }
}
1 Like

You have no idea how long i have been trying to this thank you so much for all of your help!

I take it then, that it is now working correctly? :slight_smile:

This is quite impractical as for that use is function Invoke to make it simple for you

void Begin()
{
   transform.position = new Vector3 (104,0,76);
}

now when want, like if you want that to happen 5 seconds after start, you can do this in start fucntion

void Start()
{
   Invoke("Begin", 5f);
}

Also if you want it to move every second after 5 seconds delay (just to show)
you use RepeatInvoke(“Begin”, 5f, 1f);

1 Like

Also you want it to APPEAR but this only moves it. If want to create object you can Instantiate it or if you want it to be hidden and show after 5 seconds you can set it to enabled = false; and after 5 seconds enable it.

The fact that it moves rather than appears really does not matter to much to me as long as it serves the function that i need. thank you for the suggestion though

YES it worked!! :slight_smile:

i know :slight_smile: if it works dont touch it but i just said that invoke is much easier :slight_smile:

It will be less intensive if you just enable/disable the wall instead of moving it, but this might not impact your game, depending on what your trying to make.

However, moving it is much easier, as if you enable/disable it, you will need an outside script to do so, since the wall will be disabled, so will any scripts attached to it.

The game im working on now is only a demonstration so i am not worried about it being well optimized. Next time i am making a game i will remember your suggestion though