Boost works, but now customers and packages act as boosts as well

I saw someone else had a similar issue, where packages and customers also boosted instead of acting as customers or packages. Unfortunately, their solution is not applicable to my situation. Customers are still tagged as customers, Packages are still tagged as packages, Boosts are still tagged as boosts. I did nothing to edit the delivery script, and built the driver script as prescribed in the coursework. Here’s my codes, if anyone may be able to help.

Delivery:

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

public class Delivery : MonoBehaviour
{
    [SerializeField] Color32 hasPackageColor = new Color32 (1, 1, 1, 1);
    [SerializeField] Color32 noPackageColor = new Color32 (1, 1, 1, 1);
    [SerializeField] float destroyDelay = 0.5f;

    bool hasPackage;

    SpriteRenderer spriteRenderer;

    void Start()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
    }


    void OnCollisionEnter2D(Collision2D other) 
    {
         Debug.Log("JESUS Ricky! You been drinkin?!");
    }
    void OnTriggerEnter2D(Collider2D other) 
    {
        if (other.tag == "Package" && !hasPackage) 
        {
             Debug.Log("Hope that wasn't fragile...");
             hasPackage = true;
             Destroy(other.gameObject, destroyDelay);
             spriteRenderer.color = hasPackageColor;
         }
        if (other.tag == "Customer" && hasPackage)
        {
            Debug.Log("Here's yah friggin box of crap!");
            hasPackage = false;
            Destroy(other.gameObject, destroyDelay);
            spriteRenderer.color = noPackageColor;
        }
    }
}

Driver:

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

public class Driver : MonoBehaviour
{
    [SerializeField] float steerSpeed = 200;
    [SerializeField] float moveSpeed = 15;
    [SerializeField] float slowSpeed = 1;
    [SerializeField] float boostSpeed = 30f;

    
    // Update is called once per frame
    void Update()
    {
        float steerAmount = Input.GetAxis("Horizontal") * steerSpeed * Time.deltaTime;
        float moveAmount = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;
        transform.Rotate(0, 0, -steerAmount);
        transform.Translate(0, moveAmount, 0);
    }

    void OnCollisionEnter2D(Collision2D other) 
    {
        moveSpeed = slowSpeed;
    }

    void OnTriggerEnter2D(Collider2D other) 
    {
        if (other.tag == "Boost")
        Debug.Log("Holy #@$% wuwuzzat?!");
        moveSpeed = boostSpeed;
    }
}

Same issue as last time

You need brackets

    void OnTriggerEnter2D(Collider2D other) 
    {
        if (other.tag == "Boost")
        {
            Debug.Log("Holy #@$% wuwuzzat?!");
            moveSpeed = boostSpeed;
        }
    }

Edit
When you don’t use brackets, only the next statement is considered part of the ‘if’. If you want multiple statements in the ‘if’ clause, you need to wrap them in brackets. In C#, brackets define scope. For an ‘if’, everything inside the brackets are in ‘scope’ of that ‘if’. If it’s not in the brackets, it is not in scope of the ‘if’ and therefore not affected by it. If there are no brackets, only the next statement is considered in scope because that’s the only thing the compiler can safely assume you wanted to be in scope. It cannot guess where scope should end.
So, in your code, only the Debug.Log(..) was in scope of the ‘if’. The next line that sets the speed was not in scope, and therefore not affected by the condition. This means it would execute every time anything entered the trigger because the other.tag == "Boost" is not considered anymore. The ‘if’ scope is closed.

1 Like

Me and those dang brackets. One of these days, I’ll understand the specifics of how they work. Now I don’t boost over packages, or customers anymore., thank you!

New problem though, for some reason now my delivery script no longer functions at all, despite being attached to the appropriate objects. No destruction, no print, no nothing when I run over the packages or customers. I literally changed NOTHING except providing the missing brackets you indicated in the code. Didn’t change any tags on the map. Didn’t change any scripts on the objects. Didn’t change any colliders on the objects. How can a script literally just stop functioning like that?

In your original post you indicated that the first script is on ‘Delivery’, and the second on `Driver’. Does this mean the first script is no longer on the player? Because it should be. That’s what’s driving all the delivery code.

Odd. It’s been functioning until now without the delivery script on the driver object. That fixed it though, thank you! Coding has some really strange and unclear rules. This is gonna take me a while.

Unity comes with its own concepts and rules, which are often a bit confusing for beginners. And C# has its own rules too. Don’t worry, though. You’ll get used to them if you keep developing your games with C# and Unity. We all were beginners at some point and encountered the same problems as you. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms