Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Coding Help (Code inside)

Discussion in '2D' started by SuperCrow2, Mar 19, 2018.

  1. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    So the following code advances to the next scene after clicking a button:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    public class start : MonoBehaviour
    {
    public void StartGame()
    {
    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }
    }

    However, what will the above need to change for the following to work:

    A mission where you need to collect three of the same items (same colors too) to advance to the next mission.

    Will I attach the code to the playable character?

    public void ItemsCollected()

    If (ItemsCollected==3) {
    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    }
    Else: (Do I need an else in there?

    And would any of the first 5 lines of code would need to change?
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Just because a line of code is the same, doesn't mean you have to re-use the same script. There are many times where it makes sense, but there are times - like in this example - where you can simply write the line of code, as you have there.. and it doesn't matter whether it's in the same file or not. :)

    As to whether you need an else or not, that depends on if there's more code after that inside the method, as well as whether an 'else' statement is needed for the logic you might want ;)

    I think the code might be better off on a game object that is just in the level. At least the tracking part, since each level might have different requirements. Perhaps the player can report its collection to that other script, it keeps track of what's needed (3 objects, same colour, for instance). There's more than 1 way that could work, so choose one that's comfortable for you while you're learning.
     
  3. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    Do I need to change anything for the object in the inspector panel?
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    There's an inspector panel that can be brought up for pretty much any game object/asset, so to which were you referring?
     
  5. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    The item I included in the game to collect. I didn't change anything about the object (item to collect) in the inspector panel.

    Here's my code and there is a red underline for this "ItemsCollected" it said I couldn't assign that because it is a method group.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    public class itemscollected : MonoBehaviour
    {
    // Collect Item
    private void ItemsCollected()
    {
    if (ItemsCollected = 1)
    {
    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 4);
    }
    }
    }

    There was a red underline under the entire first "if" line when I added a second equals sign.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You'll need to track the items collected from another script.

    Your error is occurring because you're trying to compare a method to an integer. :)

    If you have 'PickupManager' and each collectable item has a reference to that manager, then on the manager you can have an integer value for "pickedUpCount". Also, you can have a method "public void GotAnotherItem"
    Inside the method, add +1 to pickedUpCount and check if it's >= the number you need, and if so, change levels.

    Does that sound more like what you want?
     
  7. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    When you say 'manager" is that the same thing as "function"? That's what I learned it as :D Or is that a variable?

    If I understand you correctly, have different functions (or are they variables?) like: "PickupManager", "pickedUpCount" and "public void GotAnotherItem"

    PickupManger would be the main thing and everything else will link up to that?

    Now I am stuck because the item only gets collected when the player runs into it, then the item is "collected" it disappears. I don't know if I should attach this code to the player or to the item.

    Would I also need a "player" and "collider" variable?
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You'd probably gain a lot by going over tutorials for scripting and Unity and just working with those. Following along, and copying them out... Do this to get started.

    I really think that will be your best course of action, as it will get you acquainted with programming, and with Unity.

    The pickup manager would be a script on 1 game object in the scene.

    The picked up objects would each have their own script (the same one for each) and connect to the manager.
    -- GotAnotherItem is a function/method. And pickedUpCount is a variable.