0
0
Unityframework~20 mins

Component communication in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Component Communication Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Unity C# script?

Consider the following Unity C# script attached to a GameObject. What will be printed in the console when the game starts?

Unity
using UnityEngine;

public class Communicator : MonoBehaviour
{
    void Start()
    {
        var other = GetComponent<OtherComponent>();
        if (other != null)
        {
            Debug.Log(other.GetMessage());
        }
        else
        {
            Debug.Log("No OtherComponent found");
        }
    }
}

public class OtherComponent : MonoBehaviour
{
    public string GetMessage()
    {
        return "Hello from OtherComponent!";
    }
}
ANo OtherComponent found
BCompilation error due to missing namespace
CNullReferenceException at runtime
DHello from OtherComponent!
Attempts:
2 left
💡 Hint

Think about whether both components are on the same GameObject.

Predict Output
intermediate
2:00remaining
What does this code print when sending a message between components?

Given two components on the same GameObject, what will be printed when SendMessage is called?

Unity
using UnityEngine;

public class Sender : MonoBehaviour
{
    void Start()
    {
        SendMessage("ReceiveMessage", "Hi there!");
    }
}

public class Receiver : MonoBehaviour
{
    void ReceiveMessage(string msg)
    {
        Debug.Log("Received: " + msg);
    }
}
AReceived: Hi there!
BNo method ReceiveMessage found error
CNullReferenceException at runtime
DCompilation error due to wrong method signature
Attempts:
2 left
💡 Hint

SendMessage calls methods by name on the same GameObject's components.

🔧 Debug
advanced
2:00remaining
Why does this code cause a NullReferenceException?

Examine the code below. Why does it throw a NullReferenceException at runtime?

Unity
using UnityEngine;

public class Finder : MonoBehaviour
{
    void Start()
    {
        var other = GameObject.Find("OtherObject").GetComponent<OtherComponent>();
        Debug.Log(other.GetData());
    }
}

public class OtherComponent : MonoBehaviour
{
    public string GetData() => "Data";
}
AGetComponent<OtherComponent>() returns null because OtherComponent is missing
BGameObject.Find("OtherObject") returns null because no GameObject named "OtherObject" exists
CGetData() method is private and inaccessible
DCompilation error due to missing semicolon
Attempts:
2 left
💡 Hint

Check if the GameObject with the given name exists in the scene.

📝 Syntax
advanced
2:00remaining
Which option correctly gets a component from a child GameObject?

Choose the correct code to get a component of type ChildComponent from a child GameObject.

Avar childComp = GetComponentInChildren<ChildComponent>();
Bvar childComp = GetComponent<ChildComponent>();
Cvar childComp = transform.Find("Child").GetComponent<ChildComponent>();
Dvar childComp = GameObject.Find("Child").GetComponentInChildren<ChildComponent>();
Attempts:
2 left
💡 Hint

Use transform.Find to locate a child by name, then get its component.

🚀 Application
expert
3:00remaining
How to safely communicate between components on different GameObjects?

You want to send a message from one component to another component on a different GameObject in the scene. Which approach is best to avoid runtime errors and ensure the message is received?

AUse <code>GameObject.Find</code> to get the other GameObject, then <code>GetComponent</code> to get the component, checking for null before calling methods.
BUse <code>SendMessage</code> with the target GameObject name as a string, assuming the method exists.
CUse <code>GetComponentInChildren</code> on the current GameObject to find the other component.
DDirectly create a new instance of the other component class and call its methods.
Attempts:
2 left
💡 Hint

Think about how to find a GameObject safely and check for component presence.