Search Unity

Unity Ruby’s Adventure: 2d Beginner Official Thread

Discussion in 'Community Learning & Teaching' started by katwoolley, Apr 15, 2019.

  1. JuLz_SuN

    JuLz_SuN

    Joined:
    Jul 28, 2019
    Posts:
    4
    Your void OnCollisionEnter2D(Collision2D other) is INSIDE ur void Update Funtion. just take it out of the update function and it should work.

    your welcome
     
  2. CreateM3

    CreateM3

    Joined:
    Jul 29, 2019
    Posts:
    3
    // Start is called before the first frame update
    void Start()
    {
    rigidbody2d = GetComponent<Rigidbody2D>();
    // You just need to add this line to fix it
    animator = GetComponent<Animator>();

    currentHealth = maxHealth;
    }
     
  3. tim9202

    tim9202

    Joined:
    Jul 30, 2019
    Posts:
    2
    On part 13, Visual Styling - UI - Head-Up Display, at part 5. Scripting the Health Bar.

    The Visual Studio intellisense dosen't recognize "Image" as a keyword. I'm not sure it it's something I'm missing or what. Is there anyone else with this problem or, even better, a solution to this. I have trouble moving on to the nexts step if I can't script the UI correctly.

    Thanks
     
  4. Valjuin

    Valjuin

    Joined:
    May 22, 2019
    Posts:
    90
    Did you follow this part of the Scripting the Health Bar tutorial:
     
    tim9202 likes this.
  5. tim9202

    tim9202

    Joined:
    Jul 30, 2019
    Posts:
    2
    Apparently not close enough. Thanks a lot for pointing this out!
     
  6. JuLz_SuN

    JuLz_SuN

    Joined:
    Jul 28, 2019
    Posts:
    4
    1, You need to have made the cogBullet image into a prefab
    2. Drag and drop the cogBullet prefab into the Pubic variable you have just created in the script setting, in the Inspector.
    2. The editor is referring to the Unity Editor.

    I hope this helps
     
  7. JuLz_SuN

    JuLz_SuN

    Joined:
    Jul 28, 2019
    Posts:
    4

    Dude u have save me so much F***ing around. Thank you,10000 times thanks.
     
  8. SaltpeterTaffy

    SaltpeterTaffy

    Joined:
    Jul 27, 2019
    Posts:
    3
    I'm currently on Visual Styling - Particles. I got the particles to work just fine, but now my projectiles are not appearing in game view. As far as I'm aware, I haven't changed them in any way since that part of the tutorial so far. They're spawning in game, I just can't see them. What could have happened to cause this?

    upload_2019-7-31_2-1-5.png

    Update: I figured it out. :D The problem is that all the objects I've created have a Z of 341.55 and the camera is at Z 331, but the projectile is at Z of 0, so it's too far from the camera. I don't know why it's that way, and I couldn't find a way to change the Z of the camera(it keeps setting itself to 341.55 when I try), but I got it to work by changing the Instantiate of the projectile to

    Code (CSharp):
    1. GameObject projectileObject = Instantiate(projectilePrefab,
    2. new Vector3(rigidbody2d.position.x, rigidbody2d.position.y + 0.5f, transform.position.z),
     
    Last edited: Aug 3, 2019
  9. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    2,352
    Hi,

    This project is really good, thank you for making it! :)

    There is a slight problem in World Interactions - Collectibles Part 13. - Check Whether Ruby Needs Health. The script doesn't look right at all and it's probably most confusing for brand new beginners.

    I'm surprised nobody reported this before.

    upload_2019-8-3_18-32-21.png
     
  10. Narkotine

    Narkotine

    Joined:
    Aug 3, 2019
    Posts:
    1
    Hi, I'm stuck on part 15.4 : Exercise, where you're supposed to set all the resting sounds by yourself.
    But, in the example, you can easily call the PlaySound fonction, which is in the RubyController script because in the HealthCollectible script, things goes like this :

    void OnTriggerEnter2D(Collider2D other)
    RubyController controller = other.GetComponent<RubyController>(); (1)
    ....
    controller.PlaySound(collectedClip);

    So, I can use the PlaySound fonction in "controller" wich comes from "other" wich is the object my collectible collides with.

    But I have no idea how to do that (I mean how to use the PlaySound in RubyController) without this specific case. (for example, the cog projectile collision sound case)
    I'm pretty sure it's simple.. but I still do not know. Thanks for the help.

    And btw, if you could explain to me exactly the nature of the RubyController (s) in line noted (1), that would help.
     
    Last edited: Aug 3, 2019
  11. samia_m

    samia_m

    Joined:
    Jul 19, 2019
    Posts:
    1
    Hey everyone! I'm a beginner and I'm having issues with Chapter 7 World Interactions Collectibles - for some reason, even when I use the exact code from the tutorial, I get a console error after I implement step 10 saying that RubyController.ChangeHealth(int) is inaccessible due to its protection level? Why can't it be accessed?
     
  12. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    2,352
    Keep reading the tutorial; the answer is in the next step.
     
  13. thathurtabit

    thathurtabit

    Joined:
    Jul 29, 2018
    Posts:
    3
    I ran into this NullReferenceException after getting confused with the line:

    "You can now add that script to your Health bar GameObject, drag your mask into the Mask properties in the Inspector and enter Play mode"
    I selected the Mask (GameObject) in the Heirarchy, to make it appear in the Inspector window. Then I dragged my UIHealthBar script into the Mask Inspector window - this then shows my UI Health Bar (Script) as a component of my Mask object. I then dragged my Mask (GameObject) from the Hierarchy window *into* the Mask slot of my UI Health Bar (Script) component (In the Inspector window). My UI Health Bar (Script)'s Mask property is now set to Mask (Image) and this works when I run the game.

    This seems more complicated to explain than it is to do. Hope this helps someone.

     
  14. jwrankins

    jwrankins

    Joined:
    Aug 10, 2019
    Posts:
    2
    I could kiss you right now
     
  15. jwrankins

    jwrankins

    Joined:
    Aug 10, 2019
    Posts:
    2
    I love you
     
  16. StreetlampKid

    StreetlampKid

    Joined:
    Aug 10, 2019
    Posts:
    1
    Hi all, first off, I love this project. It's a really nice beginner's introduction to Unity. So thank you to the authors!

    Secondly, I'm having an issue with Chapter 12 - Particles. My issue is about the sort order of the particle effects. I'm able to get them to appear behind the Bot's head no problem, but they render behind everything else (photo 1). Here's a list of things I've tried:
    1. Messing with the Z-axis in the transform component of the Smoke Effect prefab and the Smoke Effect that is a child of the Bot.
    2. Changing the "Order in Layer" of the Bot and Smoke Effect in their respective render components. This successfully brings the smoke above other game objects, but also in front of the Bot (photo 2). I can also get it to display so that both the Bot and the Smoke render in front of everything else (photo 3).
    3. Change to 3D view and dragged the smoke effect as close to the foreground as possible without overlapping on the Bot.
    I know it's not game breaking or that bad, but I'd like to figure it out. There's gotta be a way, right? Thanks in advance. smokeEffectIssue1.PNG smokeEffectIssue2.PNG smokeEffectIssue3.PNG
     
    NatalieW15 likes this.
  17. NatalieW15

    NatalieW15

    Joined:
    Jul 30, 2019
    Posts:
    7
    Thanks! That solved my issue of Ruby not moving.
     
  18. NatalieW15

    NatalieW15

    Joined:
    Jul 30, 2019
    Posts:
    7
    How do I do that? I can fix it while in playmode, but can't figure out how to do that while in edit.
     
  19. Valjuin

    Valjuin

    Joined:
    May 22, 2019
    Posts:
    90
    On page 1 of this thread:

    SynolBAKA said
    Unity Gillaume replied:
    If you want to return to Lesson 8. World Interactions - Damage Zones and Enemies - Part 3, you can move the pivot point of the sprite you imported, as follows:

    - select the sprite in the Project window;
    - select Sprite Editor in the Inspector;
    - select Pivot drop-down in the Sprite Editor window;
    - select Bottom Centre

    upload_2019-8-14_16-19-19.png

    Then, when you add the BoxCollider2D, you can place it correctly.
     
    Last edited: Aug 14, 2019
    NatalieW15 likes this.
  20. Stidisram

    Stidisram

    Joined:
    Aug 14, 2019
    Posts:
    1
    Hi there.
    I think i find a bug in Unity with this lesson.
    Part 10 of tutorial: Projectiles. When i launch projectile at the corner of Comosite Collider 2d it appears as sleep: Rigidbody 2d Sleep - State State - Asleep.
    all others projectiles ok.

    i made video of it

    at 0:48 you can see bullet state, but sleeping mod state Start Awake

    sorry for my bad english
     
  21. NatalieW15

    NatalieW15

    Joined:
    Jul 30, 2019
    Posts:
    7
    You're the best - thanks! Problem solved.
     
  22. NatalieW15

    NatalieW15

    Joined:
    Jul 30, 2019
    Posts:
    7
    Here's my code, it works!

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

    public class EnemyController : MonoBehaviour
    {
    public float speed = 3.0f;
    public bool vertical;
    public float changeTime = 3.0f;
    bool broken = true;
    public ParticleSystem smokeEffect;
    public float upDownChangeInterval = 3.0f;
    float upDownTimer;

    new Rigidbody2D rigidbody2D;
    float timer;
    int direction = 1;

    Animator animator;

    // Start is called before the first frame update
    void Start()
    {
    rigidbody2D = GetComponent<Rigidbody2D>();
    timer = changeTime;
    animator = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
    timer -= Time.deltaTime;
    upDownTimer -= Time.deltaTime;

    if (timer < 0)
    {
    direction = -direction;
    timer = changeTime;

    if (upDownTimer < 0)
    {
    vertical = !vertical;
    upDownTimer = upDownChangeInterval;
    }
    }
    //remember ! inverse the test, so if broken is true !broken will be false and return won’t be executed.
    if (!broken)
    {
    return;
    }


    Vector2 position = rigidbody2D.position;

    if (vertical)
    {
    position.y = position.y + Time.deltaTime * speed * direction;
    animator.SetFloat("Move X", 0);
    animator.SetFloat("Move Y", direction);
    }
    else
    {
    position.x = position.x + Time.deltaTime * speed * direction;
    animator.SetFloat("Move X", direction);
    animator.SetFloat("Move Y", 0);
    }

    rigidbody2D.MovePosition(position);
    }
    void OnCollisionEnter2D(Collision2D other)
    {
    RubyController player = other.gameObject.GetComponent<RubyController>();

    if (player != null)
    {
    player.ChangeHealth(-1);
    }

    EnemyController e = other.collider.GetComponent<EnemyController>();
    if (e != null)
    {
    e.Fix();
    }

    Destroy(gameObject);
    }

    public void Fix()
    {
    broken = false;
    rigidbody2D.simulated = false;
    smokeEffect.Stop();
    }
    }
     
  23. NatalieW15

    NatalieW15

    Joined:
    Jul 30, 2019
    Posts:
    7
    Running into a couple issues - when robots run into Ruby or a tree/river/cog/any obstruction, they disappear. What did I do wrong?
     
  24. Valjuin

    Valjuin

    Joined:
    May 22, 2019
    Posts:
    90
    My guess is that you have added the following line to your EnemyController script within the void OnCollisionEnter2d function:

    Destroy(gameObject);

    If so, you need to delete this line.
     
    NatalieW15 likes this.
  25. el_jefes_revenge

    el_jefes_revenge

    Joined:
    Aug 17, 2019
    Posts:
    3
    Hello,

    I am working on the World Interactions part of the tutorial and I am having problems with the projectile.

    It comes into the world when I press c and moves just out of ruby's hit box, but it does not move after that. I can get it to shoot if I back ruby away from the cog collide her with it.

    In the console I get this:

    NullReferenceException: Object reference not set to an instance of an object

    When I click on it, it sends me to this part of the code:

    projectile.Launch(lookDirection, 300);

    Thank you for any help you can provide!
     
  26. Valjuin

    Valjuin

    Joined:
    May 22, 2019
    Posts:
    90
    Did you rename the void Start() function in the Projectile script to void Awake()? (See end of World Interactions - Projectile - 2. Launching the projectile)
     
  27. el_jefes_revenge

    el_jefes_revenge

    Joined:
    Aug 17, 2019
    Posts:
    3
    Yes I did
     
  28. el_jefes_revenge

    el_jefes_revenge

    Joined:
    Aug 17, 2019
    Posts:
    3
    It also doesn't play the launch animation, if that helps.
     
  29. TKisely

    TKisely

    Joined:
    Sep 3, 2017
    Posts:
    2
    Hello everybody!
    I have a tiny problem:
    If I go "behind" the boxes or the trees, it works perfectly, but I can not find out how to do the same effect with the robots, the smoke and Jambi, because when I try to go behind them Ruby is always on top of them.
    Thanks a lot!
     
  30. Valjuin

    Valjuin

    Joined:
    May 22, 2019
    Posts:
    90
    Can you post your RubyController and Projectile scripts, otherwise it will just be a guessing game?

    Please use code tags to make your code easier to read.

    Using code tags properly
     
  31. Aggrovulpes

    Aggrovulpes

    Joined:
    Jun 11, 2019
    Posts:
    2
    I've been working on trying to fix a NullReferenceExecption error I've been getting but I've not been able to fix it myself, so I was wanting to get some help. The error message I have been getting is:

    Here is the code for my Ruby Controller:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RubyController : MonoBehaviour
    6. {
    7.     public float speed = 3.0f;
    8.  
    9.     public int maxHealth = 5;
    10.     public float timeInvincible = 2.0f;
    11.  
    12.     public int health { get { return currentHealth; }}
    13.     int currentHealth;
    14.     bool isInvincible;
    15.     float invincibleTimer;
    16.  
    17.     Rigidbody2D rigidbody2d;
    18.  
    19.     Animator animator;
    20.     Vector2 lookDirection = new Vector2(1, 0);
    21.  
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.         rigidbody2d = GetComponent<Rigidbody2D>();
    26.  
    27.         currentHealth = maxHealth;
    28.     }
    29.  
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.         float horizontal = Input.GetAxis("Horizontal");
    34.         float vertical = Input.GetAxis("Vertical");
    35.  
    36.         Vector2 move = new Vector2(horizontal, vertical);
    37.  
    38.         if (!Mathf.Approximately(move.x, 0.0f) || !Mathf.Approximately(move.y, 0.0f))
    39.         {
    40.             lookDirection.Set(move.x, move.y);
    41.             lookDirection.Normalize();
    42.         }
    43.  
    44.         animator.SetFloat("Look X", lookDirection.x);
    45.         animator.SetFloat("Look Y", lookDirection.y);
    46.         animator.SetFloat("Speed", move.magnitude);
    47.  
    48.         Vector2 position = rigidbody2d.position;
    49.  
    50.         position = position + move * speed * Time.deltaTime;
    51.  
    52.         rigidbody2d.MovePosition(position);
    53.     }
    54.  
    55.    public void ChangeHealth(int amount)
    56.     {
    57.         if (amount < 0)
    58.         {
    59.             animator.SetTrigger("Hit");
    60.  
    61.             if (isInvincible)
    62.                 return;
    63.  
    64.             isInvincible = true;
    65.             invincibleTimer = timeInvincible;
    66.         }
    67.         currentHealth = Mathf.Clamp(currentHealth + amount, 0, maxHealth);
    68.         Debug.Log(currentHealth + "/" + maxHealth);
    69.     }
    70. }
    71.  
    I'm a total noob when it comes to C# and any help would be appreciated. I'd like to know:
    1. What's wrong.
    2. Why it's wrong.
    3. How to fix and be aware of the issue in the future.

    Thanks in advance!

    -----

    PS:
    It was taking a while for a moderator to review this post so I sprung for unity connect. The fix for this issue is to change the folowing code at line 19 from:

    Code (CSharp):
    1. animator animator;
    To:
    Code (CSharp):
    1. public Animator animator;
    After saving that out, then go back to Unity and do the following.
    1. Select Ruby in the hierarchy.
    2. Locate the "Ruby Controller Script" component in the inspector and drag the Ruby game object from the hierarchy and into the empty "Animator" box.



    The exception messages went away after doing this. Hopefully this will be helpful to someone else having the same problem.
     
    Last edited: Aug 19, 2019 at 8:40 AM
  32. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    2,352
    It is, at least in this precise case.

    By default in Unity C# writing a variable like this:
    Code (CSharp):
    1. Animator animator;
    makes it private. A private variable cannot be set in the inspector because it won't appear in the Inspector. It also won't be accessible from other scripts.

    To assign a value to the animator in this case, you'd have to add:
    Code (CSharp):
    1. animator = GetComponent<Animator>();
    in void Start().

    Any variable, function that doesn't begin by "public" in a C# script is automatically set "private". This is something you have to remember. :)

    I also recommend that you leave it to Visual Studio to fill in the main Unity monobehaviour functions. For example, write:
    Code (CSharp):
    1. void Sta
    and push Enter or Tab and Visual Studio will do the rest and write:
    Code (CSharp):
    1. private void Start()
    2. {
    3.  
    4. }
    Less risk to make mistakes! :)

    Edit: something else about this tutorial: do not jump here immediately when you get errors in your code, most times these errors are explained in further steps of the tutorial. ;)
     
    Last edited: Aug 18, 2019
    Aggrovulpes likes this.
  33. Aggrovulpes

    Aggrovulpes

    Joined:
    Jun 11, 2019
    Posts:
    2
    Gotcha, thank you so much for your reply I will try to keep your advice in mind in the future.
     
    APSchmidt likes this.