Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I'm actually new to programming, I had an assignment to create a single screen game and now I have to conduct tests on it. I'm completely lost as to how to conduct the test in Visual Studio. Can anyone please assist me, I've been stuck on this for about a week.

These are the tasks that I have to check:

1. The hook should drop upon click event listener.
2. The items (stone, gemstone and cash bag) should attach to hook when collided.
2. Item collection - the collected item should be disabled upon collection.
3. Score count should increase depending on the value of the collected item.
4. Displaying “You Won” message upon achieving the targeted score.
5. The game should reset after timer runs out.

These are my code for the hook:

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

public class Hook : MonoBehaviour
{
    [SerializeField]
    private Transform itemHolder;

    private bool itemAttached;

    private HookMovement hookMovement;

    private PlayerAnimation playerAnim;

    void Awake() {
        hookMovement = GetComponentInParent<HookMovement>();
        playerAnim = GetComponentInParent<PlayerAnimation>();
    }

    void OnTriggerEnter2D(Collider2D target) {

        if (!itemAttached && (target.tag == Tags.PINK_GEM || target.tag == Tags.GREEN_GEM || target.tag == Tags.BLUE_GEM
        || target.tag == Tags.ORANGE_GEM || target.tag == Tags.PURPLE_GEM
        || target.tag == Tags.LIGHT_BLUE_GEM || target.tag == Tags.PINK_GEM_SMALL || target.tag == Tags.CASH_BAG
        || target.tag == Tags.LARGE_STONE || target.tag == Tags.SMALL_STONE)){

            itemAttached = true;

            target.transform.parent = itemHolder;
            target.transform.position = itemHolder.position;
            // Set the position of the hook to the position of the item

            hookMovement.move_Speed = target.GetComponent<Item>().hook_Speed;

            hookMovement.HookAttachedItem();

            //animate player
            playerAnim.PullingItemAnimation();


            if (target.tag == Tags.PINK_GEM || target.tag == Tags.GREEN_GEM || target.tag == Tags.BLUE_GEM
                || target.tag == Tags.ORANGE_GEM || target.tag == Tags.PURPLE_GEM
                || target.tag == Tags.LIGHT_BLUE_GEM || target.tag == Tags.PINK_GEM_SMALL || target.tag == Tags.CASH_BAG)
            {

                SoundManager.instance.HookGrab_Jewel();
            }
            else if (target.tag == Tags.LARGE_STONE || target.tag == Tags.SMALL_STONE){

                SoundManager.instance.HookGrab_Stone();
            }

                SoundManager.instance.WinchCrank(true);

        } // If the target is an item

        if (target.tag == Tags.DELIVER_ITEM){

            if(itemAttached){

                itemAttached = false;
                Transform objChild = itemHolder.GetChild(0);
                objChild.parent = null;
                objChild.gameObject.SetActive(false);

                playerAnim.IdleAnimation();
                SoundManager.instance.WinchCrank(false);
            }
        }// Remove items after winching

    }// On trigger enter
}


This is for the hook movement:

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

public class HookMovement : MonoBehaviour{

    //Rotation of the hook on the z-axis
    public float min_Z = -55f, max_Z = 50f;
    public float rotate_Speed = 5f;

    private float rotate_Angle;
    private bool rotate_Right;
    private bool canRotate;

    public float move_Speed = 3f;

    // Pulling speed of the hook
    private float initial_Move_Speed;

    // How far can the hook extend
    public float min_Y = -4f;
    private float initial_Y;

    private bool moveDown;

    //For Line Renderer
    private RopeRenderer ropeRenderer;

    void Awake() {
        ropeRenderer = GetComponent<RopeRenderer>();
    }

    void Start() {

        initial_Y = transform.position.y;
        initial_Move_Speed = move_Speed;

        canRotate = true;
    }

    void Update(){
        Rotate();
        GetInput();
        MoveRope();
    }

    void Rotate(){
        if(!canRotate)
            return;

        if(rotate_Right){
            rotate_Angle += rotate_Speed * Time.deltaTime;
        } else{
            rotate_Angle -= rotate_Speed * Time.deltaTime;
        }

        //Rotate the hook in an angle on the z-axis
        transform.rotation = Quaternion.AngleAxis(rotate_Angle, Vector3.forward);

        if(rotate_Angle >= max_Z){
            rotate_Right = false;
        } else if(rotate_Angle <= min_Z){
            rotate_Right = true;
        }
    }// Rotate hook

    void GetInput(){

        if(Input.GetMouseButtonDown(0)) {

            if(canRotate) {
                canRotate = false;
                moveDown = true;
            }
        }
    }// Stop rotating the hook and move the rope

    void MoveRope() {

        if(canRotate)
            return;

        if(!canRotate) {
            SoundManager.instance.RopeWhip(true);
            Vector3 temp = transform.position;

            if (moveDown) {
                temp -= transform.up * Time.deltaTime * move_Speed;

            } else {
                temp += transform.up * Time.deltaTime * move_Speed;
            }

            transform.position = temp;
            
             if(temp.y <= min_Y) {
                moveDown = false;
            }//Move the rope up when it reaches minimum y-axis 

             if(temp.y >= initial_Y) {
                canRotate = true;

                // deactivate line renderer
                ropeRenderer.RenderLine(temp, false);

                // reset move speed
                move_Speed = initial_Move_Speed;

                SoundManager.instance.RopeWhip(false);
            }

            ropeRenderer.RenderLine(transform.position, true);
        }

    }// Move Rope


    public void HookAttachedItem(){
        moveDown = false;
    }
}


This the gameplay script:

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

public class GameplayManager : MonoBehaviour {

    public static GameplayManager instance;

    [SerializeField]
    private Text countdownText;

    public int countdownTimer = 60;

    [SerializeField]
    private Text scoreText;

    private int scoreCount;

    [SerializeField]
    private Image scoreFillUI;

    public RectTransform endScreen;
    public Text endMessage;

    void Awake() {
        if (instance == null)
            instance = this;
    }

    void Start()
    {
        DisplayScore(0);
        countdownText.text = countdownTimer.ToString();
        StartCoroutine("Countdown");
    } // Start the countdown when the game starts

    void Update()
    {
        if (Input.GetKey(KeyCode.Escape))
        {
            Application.Quit();
        }
    } // End the game when the esc button is pressed.

    IEnumerator Countdown(){

        yield return new WaitForSeconds(1f);
        countdownTimer -= 1;
        countdownText.text = countdownTimer.ToString();

        if(countdownTimer <= 10){
            SoundManager.instance.TimeRunningOut(true);
        } // Play timer sound if less than 10secs

        StartCoroutine("Countdown");

        if(countdownTimer <= 0){
            StopCoroutine("Countdown");
            SoundManager.instance.GameEnd();
            SoundManager.instance.TimeRunningOut(false);

            StartCoroutine(RestartGame());
        } // Restart the game when the timer ends

    }//Countdown timer

    public void DisplayScore(int scoreValue) {

        if (scoreText == null)
            return;

        scoreCount += scoreValue;
        scoreText.text = "$" + scoreCount;

        scoreFillUI.fillAmount = (float)scoreCount / 100f;
        // Fill the score bar

        if(scoreCount >= 100) {

            StopCoroutine("Countdown");

            endScreen.gameObject.SetActive (true);
            endMessage.gameObject.SetActive (true);
            // Active you won message

            SoundManager.instance.TimeRunningOut(false);
            SoundManager.instance.YayCharacter();
            SoundManager.instance.GameEnd();
            //Stop timer sound FX

            StartCoroutine(RestartGame());

        } // Display score

    }

    IEnumerator RestartGame(){
        yield return new WaitForSeconds(4f);

        UnityEngine.SceneManagement.SceneManager.LoadScene("Gameplay");
    } //Restart the game
}


What I have tried:

I'm completely lost...please help.
Posted
Updated 1-Nov-20 0:43am

The Unity Test Framework package (formerly the "Unity Test Runner") is a tool that allows you to test your code in both Edit mode and Play mode, and also on target platforms such as Standalone, Android, or iOS
.

For more information on other versions of the Test Framework package, see the com.unity.test-framework page.
try these: [^]

Unity Unit Testing Basics Tutorial (C#, NUnit) - Let's Make a ...letsmakeagame.net › unity-unit-testing-basics-tutorial
Jun 7, 2020 — Unit Tests are a way to be more sure of the changes you are making, a way to reduce the number of defects, catch errors as soon as possible ...

Introduction To Unity Unit Testing | raywenderlich.comwww.raywenderlich.com › 9454-introduction-to-unity-...
May 22, 2019 — How it works in Unity using the Test Runner; Writing and running unit tests that pass. Note: This tutorial assumes you are familiar with C# and ...
‎What Is a Unit Test? · ‎Looking at Example Unit Tests · ‎Writing Your First Unit Test

Unit Testing - Unity - Manualdocs.unity3d.com › Manual › testing-editortestsrunner
Debugging C# code in Unity. Scripting concepts. Unit Testing. As your project grows, and the number of scriptsA piece of code that allows you to create your own ...
‎Unity Test Runner · ‎Test Framework · ‎Editor Test Runner · ‎Español

Practical Unit Testing in Unity3D | by Kuldeep Singh ... - Mediummedium.com › xrpractices › practical-unit-testing-in-un...
Nov 17, 2019 — Unity3D tests are also of a kind of integration test where we test the complete life cycle of a game object, refer the below post on basics setup for ...
 
Share this answer
 
Comments
BillWoodruff 1-Nov-20 12:06pm    
Shayalp1 5hrs 20mins ago: "Thank you for your solution @BillWoodruff, I have been attempting it but I just need someone to guide me. This is what I have so far"

I think you could get assistance on the Unity Forum for Testing and Automation:
https://forum.unity.com/forums/testing-automation.211/?direction=desc
Thank you for your solution @BillWoodruff, I have been attempting it but I just need someone to guide me. This is what I have so far

using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;

    [TestFixture]
    public class TestSuite
    {
        private Hook hook;
        private HookMovement hookMovement;
        private bool moveDown;

    [SetUp]
                public void Setup()
        {
            GameObject gameGameObject = MonoBehaviour.Instantiate(Resources.Load<gameobject>("Prefabs/Player/Hook"));
            hook = gameGameObject.GetComponent<hook>();
        }

        [TearDown]
        public void Teardown()
        {
            Object.Destroy(hook.gameObject);
        }

        [UnityTest]
        public IEnumerator HookMovesDown()
        {
            hookMovement.moveDown = true;
            Assert.True(hookMovement.moveDown);

            yield return null;
        }
    }


But I end up getting error CS1061: 'HookMovement' does not contain a definition for 'moveDown' and no accessible extension method 'moveDown' accepting a first argument of type 'HookMovement' could be found (are you missing a using directive or an assembly reference?)
 
Share this answer
 
v2
Comments
Richard MacCutchan 1-Nov-20 6:55am    
You have declared HookMovement.moveDown as private, so the test package cannot access it. You need to either make it public, or create a public method that can manipulate its value.
BillWoodruff 1-Nov-20 12:08pm    
I think you could get assistance on the Unity Forum for Testing and Automation:
https://forum.unity.com/forums/testing-automation.211/?direction=desc
Shayalp1 1-Nov-20 15:14pm    
Okay thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900