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

Question I need some starting point to script this object spawner

Discussion in 'Scripting' started by GiulliaMoonzzi, Jun 18, 2023.

  1. GiulliaMoonzzi

    GiulliaMoonzzi

    Joined:
    Jul 9, 2022
    Posts:
    1
    Ok im not even sure if this is where im meant to ask those questions because im not super used to using forums but here it goes.

    So i have this assignment where i need to code this mobile game. Im doing a minigame that works as follows: You need to pass the iron in this piece of clothing, if you leave the iron still for too long it will burn and its game over (that part is already scripted), but as youre trying to pass the iron a little cat will keep appearing on the background and you need to stop passing the iron in order to drag the cat out of the screen otherwise they will knock the objects down and its game over.

    So, basically i dont where to start with the cat. I need make it instantiate on different sides of the screen and also like...When one is destroyed another spawned? Imagine that when you drag the cat off screen it would get destroyed, and another would be instantiated. But it also needs to be in specific spots of the screen, and also depending on where it spawns, the movement of the cat changes? The direction is going.

    Anyways i dont even know where to start. I dont know how to search for any of my doubts, i dont know what code should i be looking for, my teacher didnt teach anything the only people passing this course are people learning by themselves and im panicking. Just any light on where should i look for to find some answers would be really appreciated.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    If this is a 2D mobile game, then it should be pretty easy. You can just set up all the spawn points, randomly pick one, have the cat appear, and go from there. But, there is a lot to this stuff, and you haven't provided a lot of info to get feedback on what to look for.

    A traditional spawner really isn't going to be as useful. So, start breaking things down.

    1. Create a timer.
    2. Create a collection.
    3. When the timer hits a point, pick from the collection.
    4. and... keep going!

    Don't look at the assignment as a whole. Plan out each step and working on it a piece at a time. Then, if you get stuck on one particular piece, feel free to ask for some direction after you attempted it. Think about what parts you need. Cat needs to appear, you need a way to push it away, etc.
     
    XIV_Dis likes this.
  3. XIV_Dis

    XIV_Dis

    Joined:
    Mar 20, 2020
    Posts:
    8
    I think you should start by defining the constraints of cat spawn system. What shouldn't happen? And what must happen? When you start to define these things you will most likely imagine the spawn behaviour. And that's gonna be your starting point. For example you know that it should spawn in different spots. So you need some way to define these spots. I assume this spawn points will be pre-defined. I see most people uses empty GameObjects to define spawn points (even though I don't like this approach it's an easy way to do it for beginner) and put them in spawner's spawnPoint list or something like that. And you should probably define a duration for spawning the cat. I think we got something that we can start from and we can tweak it later on. Here is pseudo code that may guide you


    duration
    currentTime

    Update()
    {
    currentTime+= deltaTime

    if (currenTime >= duration)
    SpawnCat()
    }

    SpawnCat()
    {
    spawnPoint = GetRandomSpawnPoint()
    Instantiate(catPrefab, spawnPoint.position, Quaternion.identity)
    }

    Transform GetRandomSpawnPoint()
    {
    index = Random.Range(0, spawnPointList.Count)
    return spawnPointList[index]
    }


    I hope this helps for you to start.

    I'm on mobile so there may be somethings that I missed.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Start by asking yourself "Can I...?", just like this guy:

    Imphenzia: How Did I Learn To Make Games:



    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!


    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    Finally, when you have errors, don't post here... just go fix your errors! Here's how:

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.