Search Unity

Question Only one object activate

Discussion in 'Scripting' started by IronDragonDev, Jun 20, 2020.

  1. IronDragonDev

    IronDragonDev

    Joined:
    May 20, 2020
    Posts:
    7
    Hi,
    I'm new to unity and I have a small problem in my game when you start the game there is a tap to start screen shows and there is a homing missile that follows the player now the problem is before I even tap to start the missile start following the player I managed to make a script that disables the missile until I start but its only work for one missile, like if I have two missiles and I start the game one of the two missiles will activate and the other will not until the first one get destroyed I hope that makes sense and I'm thankful for the help,
    the code:


    public GameObject image;

    [System.Obsolete]
    public void Update()
    {
    if (!image.active)
    {
    var Script = FindObjectsOfType<Rocket>();
    Script[0].enabled = true;
    }
    }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    I would suggest you not do this in Update to begin with. You should have a system to trigger the missiles when the game starts. A better naming convention and some tweeks and you'll get a better system overall.

    I'll explain why you are getting the behavior you are.

    Quite simply, you are getting an array of objects with the Rocket script attached, but you are only activating the first Rocket in the list by accessing index 0, so if you absolutely want to do it this way, you need to loop through your Script collection and enable each Rocket in the collection.
     
  3. IronDragonDev

    IronDragonDev

    Joined:
    May 20, 2020
    Posts:
    7
    Thank you so much I'll try that