Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Opening door after collecting 8 pieces. Script help needed.

Discussion in 'Scripting' started by Ironhide, Jan 18, 2016.

  1. Ironhide

    Ironhide

    Joined:
    May 9, 2013
    Posts:
    26
    So im looking for a script that automaticly opens a door after you have collected number of items. I can animate the door by my self. Thx!
     
  2. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    If I can speak for others, generally people don't like to respond to "I'm looking for a script..."

    You should probably rephrase in the form of a question looking for guidance. We want to help you, I'd imagine, but only if you want to learn how to do it yourself.

    In the end, simply getting a script without understanding it... well, I don't think you'd ever finish your project :)

    That said, try this concept...

    Lets say there are 8 specific items you want to collect, and then stuff you want to do afterward.

    I'll call these QuestObjects, and for my games, I like to make them actual game objects that I bring into the scene, under an empty game object that I call "Quest Objects".

    Perhaps each time the player gets one of the objects, you can run a function that I'll call "CheckForObjects()"

    One of the variables on that script may be one called "objectsRequired", a String, and maybe its value is something like: "Object1,Object2,Object3,Object4,Object5,Object6,Object7,Object8"

    If you use a String.Split command, you can create an array where each value is one of those object names. Now you just have to use a for each loop to find out if the object exists in your game. (GameObject.Find is "slow", but if you only do it a few times and not every frame, it should be fine)

    If all 8 exist in the game -- IE, the player has collected all 8 -- run whatever other code you'd like, such as "OpenDoor()"

    Here's very un-tested code, written in Unity script. Do yourself a favor and re-write it if you'd like, to understand what's going on.

    Code (JavaScript):
    1. var objectsRequired : String = "Object1,Object2,Object3,Object4,Object5,Object6,Object7,Object8"; // A comma-delimited string containing the names of the objects required.
    2.  
    3. function ObjectPickedUp(){
    4.    // An object was picked up!
    5.    CheckForComplete();
    6. }
    7.  
    8. function CheckForComplete(){
    9.    // Lets see if we have all the objects.
    10.    // Keep in mind when you instantiate an object, unity adds "(Clone)" to its name -- either change the name when you instantiate it, or adjust for that here.  Probably just change the name when you instantiate it!
    11.    var objects = String.Split(objectsRequired, ",");
    12.    var totalObjects = objects.Length;
    13.    var objectsFound = 0;
    14.    for (var i : int; i < objects.Length; i++){
    15.       if (GameObject.Find(objects[i]))
    16.          objectsFound++;
    17.    }
    18.    if (objectsFound == objects.Length)
    19.    {
    20.       //Found them all!
    21.       OpenDoor();
    22.    }
    23.    else
    24.    {
    25.       print ("Only " + objectsFound + " Objects Found!");
    26.    }
    27. }
     
    Last edited: Jan 18, 2016
  3. Ironhide

    Ironhide

    Joined:
    May 9, 2013
    Posts:
    26
    Hi thx for reply!

    I get what you are saying and agree with you. I have tried to learn to do this work 5 days now with no luck. Im so tired of this script that i feel i would learn how it works if someone writes it down for me. That sounds stupid... Sry. I dont understand coding and this is just complicading things.

    So i have learned with this tutorial and have made it my self.


    I just want a script that lets me advance to next lvl after collecting every piece. For me trying to write it down at this point is making me mad... :)
     
  4. infinitypbr

    infinitypbr

    Joined:
    Nov 28, 2012
    Posts:
    3,149
    Trust me I get what you'er saying -- when I started with Unity I was TERRRRRIBLE. Even now, I'm learning things that are making me mad that I didn't learn them a few years ago. My game "The Barbarian" is full of wrong-ways of doing things, and slowly I'm fixing what I can, but it's not easy to go back.

    I'm sure you've seen it, but the learn section (http://unity3d.com/learn) is a great place to start -- I'd suggest the basic scripting lessons: http://unity3d.com/learn/tutorials/topics/scripting

    T
    he live training (1st link, bottom left option) has some great stuff including these for beginners:
    http://unity3d.com/learn/tutorials/...ning-archive/coding-for-the-absolute-beginner
    http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/creating-a-breakout-game

    I like the live training -- you can pause and play as much as you need ,and it forces you to actually WRITE the code rather than copy/paste. In each section, I'd suggest making sure you fully understand what's going on before continuing.

    I also like to then do some small custom project afterward with the same techniques I just learned, in order to solidify my understanding. Because of one of those live training sessions, for the past 5 days I've been doing (what I consider) to be kick-ass work on using classes to save & load data. I'm adding more and more features to it, and when I'm ready I'll be going to my Barbarian game and updating the save/load to this new (And "correct") system.. it'll be pretty cool once it's done :)
     
    Ironhide likes this.
  5. Ironhide

    Ironhide

    Joined:
    May 9, 2013
    Posts:
    26
    When you have worked 5 years with Unity and still can't get simple things like this to work.. It takes so much time to learn.

    But thanks for these links i'll try learning with them. Hope that hard work pay of at some point :)
    Cheers mate!
     
  6. JayJennings

    JayJennings

    Joined:
    Jun 24, 2013
    Posts:
    184
    If you're only counting the number of balls you "pick up" it should be really easy to add to the needed code. You need to do three things:

    1. Increment a counter every time you grab a ball.
    2. When that happens, check the counter to see if you reached your total.
    3. If so, load the new level.

    If you're grabbing different things and need to differentiate between them, use something like what sfbaystudios wrote.

    But if you're just grabbing the balls in Roll-A-Ball, I think you can do what you want with 4 lines of (quick and dirty) code.

    Jay
     
  7. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Scripting help should be asked for in the scripting section.

    Moving this thread.