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

2D UFO Tutorial Q&A

Discussion in 'Community Learning & Teaching' started by Matthew-Schell, Jan 22, 2016.

  1. Russellgoke

    Russellgoke

    Joined:
    Jul 23, 2017
    Posts:
    1
    I was getting this error for my code that makes the camera follow the player in Unity 2017.1:

    UnassignedReferenceException: The variable player of CompleteCameraController has not been assigned.
    You probably need to assign the player variable of the CompleteCameraController script in the inspector.
    CompleteCameraController.LateUpdate () (at Assets/Scripts/CompleteCameraController.cs:23)

    I decided to completely copy the text from the example straight into my code:

    using UnityEngine;
    using System.Collections;

    public class CompleteCameraController : MonoBehaviour {

    public GameObject player;

    private Vector3 offset;
    void Start ()
    {
    offset = transform.position - player.transform.position;
    }
    void LateUpdate ()
    {
    transform.position = player.transform.position + offset;
    }
    }
    I still got the same error, I don't know if maybe this is because I am using Unity 2017 or what
     
  2. KAYUMIY

    KAYUMIY

    Joined:
    Nov 12, 2015
    Posts:
    115
    Hello,

    You did not assign the player variable to the script. Please drag and drop the player gameObject.
    I think your Player field is empty in the Inspector of the main Camera.
    dsadas.jpg
     
    Maximakkara likes this.
  3. KAYUMIY

    KAYUMIY

    Joined:
    Nov 12, 2015
    Posts:
    115
    You opened the Completed version of the game. You should do from scratch by following the tutorial.
     
  4. Chayoa

    Chayoa

    Joined:
    Jul 27, 2017
    Posts:
    1
    Hi,
    As I tried to move the UFO, nothing happened. I checked the Code, but I didn't find any errors.
     
  5. KAYUMIY

    KAYUMIY

    Joined:
    Nov 12, 2015
    Posts:
    115
    Please check the UFO inspector as well if there is nothing wrong in the script.
    dasd.jpg
     
  6. ayayront

    ayayront

    Joined:
    Jul 31, 2017
    Posts:
    1
    I'm on the "Following the Player with the Camera" section and upon attaching the main camera to the player as a child, the camera preview goes black and entering play mode just shows a black screen. Any idea how to fix this?
     

    Attached Files:

  7. KAYUMIY

    KAYUMIY

    Joined:
    Nov 12, 2015
    Posts:
    115
    You don't need to attach the main camera to the Player as a child. Keep Watching the section. And then you will find better solution.
     
  8. motoslau

    motoslau

    Joined:
    Aug 27, 2017
    Posts:
    2
    Hello, i have read this forum thread however i could not find my problem listed here.
    Here's my code:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class PlayerMovement : MonoBehaviour {
    public float speed;

    public Text countText;

    private Rigidbody2D rb2d;

    private int Count;

    void Start()
    {
    rb2d = GetComponent<Rigidbody2D> ();
    Count = 0;
    SetCountText();
    }

    void FixedUpdate()
    {
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");
    Vector2 movement = new Vector2(moveHorizontal, moveVertical);
    rb2d.AddForce(movement * speed);
    }

    void OnTriggerEnter2D(Collider2D other)
    {
    if (other.gameObject.CompareTag("PickUp"))
    {
    other.gameObject.SetActive(false);
    Count = Count + 1;
    SetCountText();
    }
    void SetCountText()
    {
    CountText.text = "Count: " + Count.ToString();
    }
    }
    }
    Now the problem is that the "Count Text" slot is not displayed in Unity.
    I have tried capitalizing and writing with lower case.
    I initially had an error in regards to C# 7 as the SetCountText function apparently does not work when using C# 4.
    I changed the default to C# 7 for all projects and restarted Visual Studio 2017.
    The script is saved several times over :)
    I am new to coding and this is the first thing i have decided to try.
    I'm running Unity 2017.1.0f3 Personal Edition.

    Thank You very much for the wonderful and very helpful and easy to understand tutorial.
    Have a nice day! :D
     
  9. motoslau

    motoslau

    Joined:
    Aug 27, 2017
    Posts:
    2
    Aaaaand, im an idiot, that took 5 minutes to figure out :)) my void SetCountText was within the OnTriggerEnter2D :p
    Awesome tutorials guys, i should just pay more attention i guess :p
     
  10. KAYUMIY

    KAYUMIY

    Joined:
    Nov 12, 2015
    Posts:
    115
    Did you find solution?
     
  11. gwnguy

    gwnguy

    Joined:
    Apr 25, 2017
    Posts:
    144
    Glad you found it.
    I was thinking the problem was you defined it
    public Text countText;
    but referenced it
    CountText.text = "Count: " + Count.ToString();

    Note the lowercase c vs capital C in variable countText. I'm surprised that worked
     
  12. FerreTrip

    FerreTrip

    Joined:
    Sep 4, 2017
    Posts:
    1
    Hi, I'm someone who sadly doesn't have constant access to the Internet. Is there any way I can get an offline version of this tutorial? Like an archive with the video files? I have the assets already.
     
  13. Tigersong

    Tigersong

    Joined:
    Aug 11, 2014
    Posts:
    73
    I'm on the fourth video, "Adding Collision", and just switch to 2017.2.0b10. I've put all the colliders in the right positions, but on pressing Play the UFO still slides to the bottom of the screen. Each collider has a message in the Inspector: "This collider will not function with a composite until there is a CompositeCollider2D on the GameObject that the attached Rigidbody2D is on." Help?
     
  14. Tigersong

    Tigersong

    Joined:
    Aug 11, 2014
    Posts:
    73
    I'm working on the PlayerController script (typing what's in the completed script- I love Atom!) and need a little help with the indentation. Here's the problem section:

    Code (csharp):
    1. //Initialize count to zero.
    2.    count = 0;
     
  15. KAYUMIY

    KAYUMIY

    Joined:
    Nov 12, 2015
    Posts:
    115
    What is your problem?

    can you explain in detail?
     
  16. JacekMackiewicz

    JacekMackiewicz

    Unity Technologies

    Joined:
    Jul 5, 2017
    Posts:
    34
    Thanks for the suggestion for offline tutorials and for bringing it to our attention! We don't have any plans right now to hold our own downloadable library of video tutorials, but it is something we might investigate.
     
    KAYUMIY likes this.
  17. Tigersong

    Tigersong

    Joined:
    Aug 11, 2014
    Posts:
    73
    Is there any chance of getting the video tutorials converted to text? Not everyone likes to jump back and forth through a video, hoping to get things exactly right.
     
    LabGecko likes this.
  18. Brian-Washechek

    Brian-Washechek

    Joined:
    Aug 5, 2015
    Posts:
    846
    I have no clue why I can't pick up my gold nuggets. They're spinning and I can fly right over them, but not pick them up. I've been trying to figure this out for a little while now.

    I made it to 16:15 in the tutorial and it was looking OK until this happened. You could look at my script and tell me based on that, but I have access to an area for upload if you want me to upload the entire project. I uploaded my assets if you would like to look at them and tell me where my error is.

    I would like to know what would be the most efficient way for you all to see where and how I messed up.
     

    Attached Files:

    Last edited: Oct 1, 2017
  19. Brian-Washechek

    Brian-Washechek

    Joined:
    Aug 5, 2015
    Posts:
    846
    Haha!
    Code (csharp):
    1.  
    2. if (other.gameObject.CompareTag ("PickUp"))
    3.  
    looks like
    Code (csharp):
    1.  
    2. if (other.gameObject.CompareTag ("Player"))
    3.  
    in the tutorial. Or maybe I just need to clean my glasses. Anyway, the problem has been solved.
     
  20. Brian-Washechek

    Brian-Washechek

    Joined:
    Aug 5, 2015
    Posts:
    846
    My UFO used to rotate when it hit walls. Now it doesn't. I somehow switched that off. What do I switch back on?.
     
    Last edited: Oct 3, 2017
  21. JacekMackiewicz

    JacekMackiewicz

    Unity Technologies

    Joined:
    Jul 5, 2017
    Posts:
    34
    Hi Tigersong,

    While there are currently no plans to convert all of our video tutorials into text, however, if you go to the tutorials through https://unity3d.com/learn/tutorials they all include transcripts (with timestamps) alongside all the code snippets discussed in the video below the video player.
    Hope this helps!
     
  22. GunDizer

    GunDizer

    Joined:
    Oct 12, 2017
    Posts:
    1
    Hello, Thanks for the great tutorial, I completed it but I have a question regarding the WinText.

    Why when the WinText show up it tracks the player around, on the other hand the CountText is still in its place?
     
  23. Brian-Washechek

    Brian-Washechek

    Joined:
    Aug 5, 2015
    Posts:
    846
    My UFO used to spin when it hits a wall. It used to and I unintentionally disabled it. Could you inform me how to re-enable it?
     
  24. Navigatr

    Navigatr

    Joined:
    Nov 9, 2017
    Posts:
    1
    I think that maybe this tutorial needs an update or something, because I cannot for my life figure out what's wrong.

    I'm on the third video, with the PlayerController script. However, when I click play, the player gets pulled off the board towards the top left. Yes, I've double checked capitalization and what not in the script, and even copied the one provided on the tutorial page. Yes, gravity is absolutely set exactly to 0. I've done this tutorial before in an older version of Unity, where it worked perfectly fine, but now for some reason it doesn't. The only reason I can figure out is that there would be a different default setting somewhere, which seems strange to me to start with.

    EDIT:
    So I managed to figure it out on my own. Apparently, in the project settings (Edit > Projects Settings > Input) the "Horizontal" and "Vertical" properties relating to both the keyboard/mouse and the ones for handcontrollers were named exactly the same. When I changed the names for the latter ones to "Horizontal Joystick" and "Vertical Joystick" the problem went away and everything works fine now. Hope this might help someone else as well.
     
    Last edited: Nov 9, 2017
  25. redf150flames

    redf150flames

    Joined:
    Nov 12, 2017
    Posts:
    4
    Did you resize the canvas somewhere before the 11:15 mark that wasn't in the video? Seems odd to me that its way bigger than the camera area (like at the beginning of the video) and I think its causing my Win Text to be off screen.
     
  26. Nishinian

    Nishinian

    Joined:
    Mar 19, 2017
    Posts:
    5
    VIDEO 3/9

    I'm gonna be honest, I didn't check all 175 posts to see if there's an answer to my question, which goes as following:

    I don't really fully GET the code we put in FixedUpdate ()

    I don't understand how 'words' like moveHorizontal and moveVertical become forces that move X & Y in the AddForce method.

    This is how I see it:
    1. We tell moveHorizontal and moveVertical to find and access the horizontal and vertical arrow keys
    | effectively to 'be' those keys, right?
    2. We take both words and put them in a Vector2, like saying 'now you are X, and you are Y'. And BOTH these axes are ONE word called 'movement'
    | This is where I'm starting to get lost.. are we telling that these horizontal and vertical keys are actually the points on the X and Y axes?
    3. We tell the rigidbody that it gets force added to it, on both the x & y axes because we put 'movement' where the Vector2 is supposed to go.
    I In my mind, we only talked about locations (in the 2D graph) up until now, so how in the hell is the player GO suddenly moving? Just because of the default force that we ommitted in the AddForce function?

    And because of all this location-talk, I never thought of just multiplying the float speed (=in my head, a force) with 'movement' (=locations like vertical & horizontal, x & y points). Thus it seemed to me this wouldn't add up, like multiplying a dynamic thing like speed with a static thing like a location. (e.g.: 10 * 0 = 0, zero speed)
    I thought all the 'force' belonged behind the comma, in the second part of the AddForce function. Apparently, my logic didn't add up.

    I don't know if I'm clear enough but I feel like I just need like a snippet of the right explanation to get me to fully understand it. Anyone up for it? :)
     
  27. yunhol92

    yunhol92

    Joined:
    Nov 17, 2017
    Posts:
    3
    Hi all,

    I keep having this problem when I code the controller for player. Would like to have your help to see my programme.

    upload_2017-11-17_14-43-34.png
     

    Attached Files:

  28. sommyD

    sommyD

    Joined:
    Nov 23, 2017
    Posts:
    1
    first create a folder called "Scripts" and move your PlayerController script into it. Then drag your script into the inspector window, look you have nothing selected there.
     
  29. unity_9mKBFP6CoR7kLg

    unity_9mKBFP6CoR7kLg

    Joined:
    Nov 30, 2017
    Posts:
    1
    Hi guys,

    I have a problem with the Win Text.

    It doesn't appear in the middle of the screen.

    Instead, the Score count changes to "You Win!"

    I looked through my script, and how i set up the UI. I did everything exactly as shown in the tutorial... Any clues what I do wrong?
     
  30. Renoten

    Renoten

    Joined:
    Jan 24, 2018
    Posts:
    1
    I'm having an issue where the console doesn't recognize rb2d. This is my code.
    Screen Shot 2018-01-23 at 6.19.02 PM.png
     
    KAYUMIY likes this.
  31. gwnguy

    gwnguy

    Joined:
    Apr 25, 2017
    Posts:
    144
    Put a semi-colon at the end of line 17 (to end that line of code)
     
    KAYUMIY likes this.
  32. DucaDiMonteSberna

    DucaDiMonteSberna

    Joined:
    Jan 18, 2018
    Posts:
    79
    I have a problem concerning the game build:

    when I test the game in unity editor, all is going as expected,
    but when I play the game from a build standalone version, I have that my player cannot interact with some of the pick ups I made (only a category of them, i added many different types an an exercise)

    EDIT: nevermind, closing and reopening unity and rebuilding tha game solved that
     
    Last edited: Mar 28, 2018
  33. Northernlightifly

    Northernlightifly

    Joined:
    Feb 26, 2018
    Posts:
    71
    I watch your turtorial and it's really good! I have a question about the collision. I want to use it on the background like you did and I also want to do it on my wall sprites am I able to do that? And do it exacly like you did but I just mark my sprites when I add collision? I want my walls to act like hinder and will dissappear when you press a button.
     
  34. Spacehike

    Spacehike

    Joined:
    Jun 19, 2013
    Posts:
    1
    If PickUp is not working:

    Go to the PickUp prefab under the Circle Collider 2D component, and make sure the "Is Trigger" box is checked.
     
  35. Northernlightifly

    Northernlightifly

    Joined:
    Feb 26, 2018
    Posts:
    71
    Okay thank you! I was also wondering I want my game to quit when my ball pass the finish flag, I have attached colliders on both objects and set trigger on the flag and added rigidbody 2d on my ball, I don't know what else to do, do you know?
     
  36. Veron1208

    Veron1208

    Joined:
    May 11, 2018
    Posts:
    1
    I have this problem I started making this UFO game a day ago and it worked fine on the testing application but when I turned it into a build and ran the game the Player only hovers over the gold this also the same with the complete game asset is my application corrupt or is there something I'm doing wrong?

    the player collects nuggets in the making phase but as I run the first build the player only hovers over the nuggets, same with the complete game asset I built ,it also hovers over the nugget

    EDIT: Nuggets nevermind same with oldghost stalker I was able to run my game fine
     
    Last edited: May 17, 2018
  37. carlo808

    carlo808

    Joined:
    May 25, 2018
    Posts:
    2
    Did you get a resolution to this?
     
  38. carlo808

    carlo808

    Joined:
    May 25, 2018
    Posts:
    2
    Hi Tigersong
    Did you get a resolution to this?
    cheers
    Carl
     
  39. Tigersong

    Tigersong

    Joined:
    Aug 11, 2014
    Posts:
    73
    No, I haven't solved it. P'raps it's time to research CompositeCollider2D.
     
  40. fireshots

    fireshots

    Joined:
    Jun 19, 2018
    Posts:
    1
    Hi I’m beginner and I have question . I can’t figure out why my player doesn’t collide with pick up objects? I followed every step in tutorial , and I turned on its triggered for pick ups i tagged them I turned on kinematic as well. Please help if you can I don’t want to ditch this project I come so far.
     
  41. sstclair

    sstclair

    Joined:
    Jan 7, 2018
    Posts:
    1
    I am not getting the pickups to show in the build.
     
  42. rhoen2018

    rhoen2018

    Joined:
    Jul 2, 2018
    Posts:
    1
    Thank you for a great tutorial, I'm experiencing a bug in my program on the compile step. When I press the play button inside the editor, the game plays as expected, pickups disappear and the counter increments. When I compile the game and play the UFO just flies over the pickups, they don't disappear, and the counter does not increment.

    Any guesses as to what might be going on would be appreciated, advice on how to generate/access logs on the compiled game might also be helpful.
    Thank you!

    Screenshot 2018-07-01 23.18.57.png Screenshot 2018-07-01 23.20.05.png
     
  43. grabmoss

    grabmoss

    Joined:
    Jul 27, 2018
    Posts:
    1
    First of all i want to thank you for making the game and the tutorial, they both really simple and clear perfect for a beginner like me!
    I'm stuck at lesson 3. my player don't react to me pressing the arrow keys. i checked and double checked and triple checked the script, i tried to move things around and finely i deleted the whole thing and just copy it word by word from the video.
    nothing helps. i think it's connected to reason the script editor don't highlight some words it needs to highlight, but i don't know how to solve that.
    I'm using Microsoft visual editor if it helps. code.jpg
     
    SpiritKitty likes this.
  44. lauhoksau0601

    lauhoksau0601

    Joined:
    Aug 23, 2018
    Posts:
    1
    I can't make because the code is error.
     
  45. SpiritKitty

    SpiritKitty

    Joined:
    Sep 7, 2018
    Posts:
    1
    I'm having this same issue is there no solution? I think it might be due to unity 5 but idk I'm new.
     
  46. Pixative

    Pixative

    Joined:
    Sep 14, 2018
    Posts:
    4
    Trying to do this on the latest version of Unity, I get errors. When trying to add the first script, with just the movement but before adding speed, it says, "Can't add script behaviour TMP_CoroutineTween. The script needs to derive from MonoBehaviour!"

    If I try and apply the finished movement script provided with the project to the player I get:
    NullReferenceException: Object reference not set to an instance of an object
    CompletePlayerController.Start () (at Assets/_Completed-Assets/Scripts/CompletePlayerController.cs:26)

    The tutorials are on youtube. youtube-dl works
     
  47. theoriginalslothsin

    theoriginalslothsin

    Joined:
    Sep 28, 2018
    Posts:
    1
    Hi all~
    So for a class assignment, we are using this tutorial to get the basics down. Part of my project is to add another type of pick-up to the game.

    So I am trying to set up different counters for my two different pick ups. I have them both set up properly as per the tutorial, but whenever I pass over any star, doesn't matter which, both counters go up. I am unsure of how to fix this. I've tried everything online it says, but I am at a loss.

    I've attached my script if this helps any.

    Any guidance is very much appreciated.
     

    Attached Files:

  48. bcmorris

    bcmorris

    Joined:
    Jan 19, 2014
    Posts:
    5
    @ Matthew-Schell,

    Question 1)
    I am a teacher and I am wondering what the policy is on using the Unity "2D UFO Tutorial"
    as a project within my game design class curriculum?

    Oh nevermind question 1, I see other people our using it in there class.

    Question 2)
    I noticed that it has been updated to Unity 5, but it's still a bit out of date. Any plans on updating it to Unity 2018?

    Question 3) Plus I would like the tutorial to be in the Windows version so,..can I use the assets and recreate the tutorial myself in Unity 2018? This would be very helpful since I could then give my (young) students two options

    Regards,
    Brian
     
    Last edited: Sep 29, 2018
  49. SnowMew

    SnowMew

    Joined:
    Dec 5, 2018
    Posts:
    1
    I'm having a really hard time downloading the assets. I've added it to my account but when I click the pink "Open in Unity" button, Unity launches with no awareness of the UFO tutorial. What am I doing wrong?
     
  50. Deleted User

    Deleted User

    Guest

    Don't click on this button. Open Unity, then the Store, click on the small "My Assets" folder and the assets you have added to your account will be there. :)