Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Feedback Code not working

Discussion in 'Getting Started' started by NohglSebas, Jul 15, 2020.

  1. NohglSebas

    NohglSebas

    Joined:
    Jul 3, 2020
    Posts:
    22
    Hey I'm making my fist real game, it's a mobile game about a bird falling and dodging stuff.

    I'm following a tutorial about making a infinite level from. Only the problem is I have to make it to go down and not with platforms but with levelparts. I alsmost got it to work but weirdly, the levelparts are getting spawned diagonally as you can see in the video.

    This is the script I use:

    public GameObject Part1;
    public GameObject Part2;
    public GameObject Part3;
    public GameObject Part4;
    public GameObject Part5;
    public GameObject Part6;
    public GameObject Part7;
    public GameObject Part8;
    public GameObject Part9;
    public GameObject Part10;
    public Transform generationPoint;
    public float heightBetween;


    void Start()
    {

    }


    void Update()
    {
    if(transform.position.y > generationPoint.position.y)
    {
    transform.position = new Vector3(transform.position.y + heightBetween, transform.position.x, transform.position.z);

    Instantiate(Part1, transform.position, transform.rotation);
    }
    }
    }
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,887
    Please use the "Code:" button in the editing toolbar when pasting code.

    I see you have
    Code (csharp):
    1. transform.position = new Vector3(transform.position.y + heightBetween, transform.position.x, transform.position.z);
    passing in Y, X, and Z. This is different from the usual X, Y, Z order. Could this be the problem?
     
  3. NohglSebas

    NohglSebas

    Joined:
    Jul 3, 2020
    Posts:
    22
    @JoeStrout
    If I do that they all just spawn in lines to the left
     
  4. NohglSebas

    NohglSebas

    Joined:
    Jul 3, 2020
    Posts:
    22
    Code (CSharp):
    1. [SerializeField] private Player player;
    and the visualstudio doesnt even understand Player anymore? idk why
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,887
    As they would, if you're telling them all the X values are just transform.position.x. If you want them to spread out, then you should add something to that.

    The correct order really is X, Y, Z, and I'm having trouble imagining any circumstances under which you would want to swap those. It seems like you're just trying to code experimentally — try something (like swapping X and Y) without understanding or thinking through what that means, and just hoping it works. That's not a very effective way to code, because the space of all possible programs is astronomically huge. You're not likely to hit on correct code this way.

    So back up, take a breath, and really think about where you want your parts to spawn. I can't tell you what the correct code is because you haven't described what you want. So start by clearly describing, at least to yourself, where one of these things should appear. Then translate that into math.
     
  6. NohglSebas

    NohglSebas

    Joined:
    Jul 3, 2020
    Posts:
    22
    @JoeStrout Your right, I'm really new to unity and I thought I could just follow a tutorial and change some stuff and it would work.
    I want the level parts to spawn under the character, in a straight line in a way that u have an infinite faller. I've tried 2 tutorials,
    and
    . But in both tutorials encounter different problems. In the first way with this code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LevelGenerator : MonoBehaviour
    6. {
    7.     public GameObject Part1;
    8.     public GameObject Part2;
    9.     public GameObject Part3;
    10.     public GameObject Part4;
    11.     public GameObject Part5;
    12.     public GameObject Part6;
    13.     public GameObject Part7;
    14.     public GameObject Part8;
    15.     public GameObject Part9;
    16.     public GameObject Part10;
    17.     public Transform generationPoint;
    18.     public float heightBetween;
    19.  
    20.    
    21.     void Start()
    22.     {
    23.        
    24.     }
    25.  
    26.    
    27.     void Update()
    28.     {
    29.         if(transform.position.y > generationPoint.position.y)
    30.         {
    31.             transform.position = new Vector3(transform.position.y + heightBetween, transform.position.x, transform.position.z);
    32.  
    33.             Instantiate(Part1, transform.position, transform.rotation);
    34.         }
    35.     }
    36. }
    I get them to spawn diagonally as u can see in the video above. I don't know how to spawn them in one line, so tried the other tutorial.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LevelGenerator2 : MonoBehaviour
    6. {
    7.     private const float PLAYER_DISTANCE_SPAWN_LEVEL_PART = 200f;
    8.  
    9.     [SerializeField] private Transform levelPart_Start;
    10.     [SerializeField] private Transform levelPart_1;
    11.     [SerializeField] private Player player;
    12.  
    13.  
    14.     private Vector3 lastEndPosition;
    15.     private void Awake()
    16.     {
    17.         lastEndPosition = levelPart_Start.Find("EndPosition").position;
    18.         SpawnLevelPart();
    19.         int startingSpawnLevelParts = 5;
    20.         for (int i = 0; i < startingSpawnLevelParts; i++)
    21.         {
    22.             SpawnLevelPart();
    23.         }
    24.     }
    25.    
    26.     private void Update()
    27.     {
    28.         if(Vector3.Distance(player.Getposition(), lastEndPosition) < PLAYER_DISTANCE_SPAWN_LEVEL_PART)
    29.         {
    30.             SpawnLevelPart();
    31.         }
    32.     }
    33.     private void SpawnLevelPart()
    34.     {
    35.        Transform lastLevelpartTransform = SpawnLevelPart(lastEndPosition);
    36.         lastEndPosition = lastLevelpartTransform.Find("EndPosition").position;
    37.     }
    38.     private Transform SpawnLevelPart(Vector3 spawnPosition)
    39.     {
    40.        Transform levelPartTransform = Instantiate(levelPart_1, spawnPosition, Quaternion.identity);
    41.         return levelPartTransform;
    42.     }
    43.  
    44.  
    45.  
    46.  
    47. }
    48.  
    49.  
    This code isn't finished but I got a couple problems really early. I got this error: NullReferenceException: Object reference not set to an instance of an object LevelGenerator.Start () (at Assets/Scripts/LevelGenerator.cs:15)
    and when I moved on I got the problem that unity wasn't reconizing
    Code (CSharp):
    1. [SerializeField] private Player player;
    And said type or namespace "Player" could not be found. I would love it if you could help and thanks for replying :).
     
  7. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,887
    No, you can't just mash together parts from different tutorials. They're each likely to reference other parts that you don't have.

    Go back to your first code, and correct the order of the parameters (x, y, and z).

    Do you understand what X and Y mean in your program?
     
  8. NohglSebas

    NohglSebas

    Joined:
    Jul 3, 2020
    Posts:
    22
    @JoeStrout No I didn't mash them, I just tried them both. I've changed it but now this happens :
     
  9. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,887
    You haven't answered my questions. Nor have you actually asked any (I don't understand what you're trying to show with your video.) The best way to get help is:

    • post a clear but concise description of what you want to happen, what is actually happening, and how that differs from what you want
    • describe what you have already tried to understand the problem
    • if asked questions by someone trying to help you, answer them is clearly & completely as you can.
     
  10. NohglSebas

    NohglSebas

    Joined:
    Jul 3, 2020
    Posts:
    22
    @JoeStrout I want to make different handcrafted level parts and have them spawn randomly below the player when he goes down. The player needs to dodge those obstacles like moving enemies and spikes by moving left and right. First I thought maybe I could swap the x and y but when I did that the parts spawned next to the player in a diagonal line. so that didn't work. U said that the order was x, y, z but what happens now is that the level parts spawn in a infinite loop to the left. But I want them to spawn under the player instead of next to the player. I want to to that with a generation point that is attached to the main camera, so if the the player falls down, a new levelpart spawns.



    I'd also like to destroy the parts that are above the player. And
    I'd also like to make the part that the levelpieces get chosen random.
     
  11. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,887
    OK, so assuming your camera setup is fairly standard, X is the horizontal coordinate — how far the objects are to the right of the origin (0,0,0). And Y is the vertical coordinate: how far the objects are up from the origin. Z would be distance from the camera; if you're making a 2D game, you basically don't need to worry about Z.

    You seem to be instantiating level parts from "LevelGenerator" script? And it's using its own position to determine where to spawn things, is that correct? What is that script attached to?
     
  12. NohglSebas

    NohglSebas

    Joined:
    Jul 3, 2020
    Posts:
    22
    I have a LevelGenerator script, and a slot for a generationpoint. I attached the generation point on my camera and i've lowered it so they spawn under the player. I want the levels to spawn on the generation point. As you can see on the photo. I have a LevelGenerator object but that doesn't really have to move or do anything.
     
  13. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,887
    OK, that's a good way to go about it.

    But since you are saying (correctly) that the position of your LevelGenerator object doesn't matter, its could should not ever be using transform.position for anything. In this code, transform.position would be the position of the LevelGenerator. So take that out.

    Looking at the script at the top of this thread, it seems to me there are two questions you need to be clear about: (1) when should you generate a new part, and (2) where do you want to generate it? The answer to (2) is pretty clear now: you want to put them at the generation point. That's as easy as

    Code (CSharp):
    1. Instantiate(Part1, generationPoint.position, generationPoint.rotation);
    It's still not clear to me what the answer to (1) is, though. When exactly should a new part be generated?
     
  14. NohglSebas

    NohglSebas

    Joined:
    Jul 3, 2020
    Posts:
    22
    Thank you, stupid I didn't think of that.
    The parts spawn under the player like I wanted. But now they all just stacking up on eachother. That's why I thought of "HeightBetween". But that doesn't seem to be working. The other tutorial used empty objects named: endposition and startposition that were placed on every prefab on the right position. That would make it less boring because the space between the parts wouldn't always be the same. But I don't know how to work with that.
    So the anwser on your first question will be just before the player gets near an empty space without obstacles.
    And one more big thank you, I've asked all kind of questions on different unity forums or on youtube video's but your the only one patient enough with me:)
     
  15. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,887
    Perhaps you want to add some randomness to the position? Try something like this (breaking it down into multiple steps to make it easier):

    Code (CSharp):
    1. Vector3 pos = generationPoint.position;
    2. pos.x += Random.Range(-10f, 10f);
    3. Instantiate(Part1, pos, generationPoint.rotation);
    Adjust that Range(-10f, 10f) for whatever range of values you want to apply. And this is assuming you want the randomness in the X (horizontal) position; if you want it in Y instead, then add to pos.y instead of pos.x.
     
  16. NohglSebas

    NohglSebas

    Joined:
    Jul 3, 2020
    Posts:
    22
    It doesn't work because there isn't a space between the parts that get spawned. The parts just all stack up on eachother. We have to find away to make sure there is a space between the parts. I tried
    Code (CSharp):
    1. pos.y += Random.Range(-5f, -1f) + HeightBetween);
    but that didn't stop the parts from spawning on eachother.
     
  17. NohglSebas

    NohglSebas

    Joined:
    Jul 3, 2020
    Posts:
    22
    But it is getting closer to what I want :)
     
  18. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,887
    I suspect the problem isn't where you're spawning the parts, but when you're spawning them. You said this should be "just before the player gets near an empty space without obstacles," but that's not clear or specific enough to translate into code yet. So you're probably spawning a new platform on every frame, or something like that.

    So maybe let's focus on that now. Imagine you're the computer; you work fast but you're very literal-minded and not too bright. How would I explain to you exactly when you should spawn a new part?
     
  19. NohglSebas

    NohglSebas

    Joined:
    Jul 3, 2020
    Posts:
    22
    Ehhm when the player is from an specific distance from the empty space. So maybe with endpositions on the levelparts.
    Or when the generationpoint is on a empty space and with the right distance from the last levelpart.
    I'm going on vacation so is it ok if we continue this conversation when i'm back? Thank you for replying.
     
  20. NohglSebas

    NohglSebas

    Joined:
    Jul 3, 2020
    Posts:
    22
  21. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,887
    OK. Have you achieved any new clarity on exactly how to tell when it's time to spawn a new part?
     
  22. NohglSebas

    NohglSebas

    Joined:
    Jul 3, 2020
    Posts:
    22
    I don't know anything else than what I said in the comment above.
     
  23. NohglSebas

    NohglSebas

    Joined:
    Jul 3, 2020
    Posts:
    22
    Do you know anything else than this or is this good? When the player is from an specific distance from the empty space. So maybe with endpositions on the levelparts.
    Or when the generationpoint is on a empty space and with the right distance from the last levelpart.
     
  24. NohglSebas

    NohglSebas

    Joined:
    Jul 3, 2020
    Posts:
    22
    Sorry @JoeStrout I don't want to be annoying but if your on vacation or have anything else, can you tell me when u will be available again?
     
  25. NohglSebas

    NohglSebas

    Joined:
    Jul 3, 2020
    Posts:
    22
    @JoeStrout I had an idea to add an invisible startpart collider on every levelpart. And add a collider on the generationpoint. So if they collided it will instantiate the levelparts. I don't know how to get the generationpoint component all the way from the Levelgenerator. So I just added the script on the generationpoint. It doesn't seem to be working :(. Any idea why?
    Code (CSharp):
    1. {
    2.     public GameObject Part1;
    3.     public GameObject Part2;
    4.     public GameObject Part3;
    5.     public GameObject Part4;
    6.     public GameObject Part5;
    7.     public GameObject Part6;
    8.     public GameObject Part7;
    9.     public GameObject Part8;
    10.     public GameObject Part9;
    11.     public GameObject Part10;
    12.     public Transform GenerationPoint;
    13.     public Transform GenerationPointtransform;
    14.     private Vector3 heightBetween;
    15.    
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.      
    20.         //GenerationPoint = GameObject.Find("GenerationPoint");
    21.        
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.        
    28.     }
    29.     private void OnTriggerEnter2D(Collider2D collision)
    30.     {
    31.         if (collision.gameObject.tag == ("StartPart")){
    32.             heightBetween = new Vector3(0, Random.Range(- 5f, -1f), 0);
    33.             Vector3 pos = GenerationPoint.position;
    34.             Instantiate(Part1, pos - heightBetween, GenerationPoint.rotation);
    35.         }
    36.  
    37.         }
    38.     }
    39.  
     
  26. NohglSebas

    NohglSebas

    Joined:
    Jul 3, 2020
    Posts:
    22
    Can anyone else help me with this matter? Debug.log isn't even working. Can someone find any mistakes in this code?
    Code (CSharp):
    1. private void OnTriggerEnter2D(Collider2D collision)
    2.     {
    3.         if (collision.gameObject.tag == "StartPart")
    4.         {
    5.            
    6.             Debug.Log("HEY");
    7.             heightBetween = new Vector3(0, Random.Range(- 5f, -1f), 0);
    8.             Vector3 pos = GenerationPoint.position;
    9.             Instantiate(Part1, pos + heightBetween, GenerationPoint.rotation);
    10.         }
    11.  
    12.         }
    13.     }
     
unityunity