# Coding Challenges

Updated on April 28, 2017 in
10 on May 13, 2015

Hello everyone,

now and then I see people asking for something to program, or something to learn.
So to help I have written down some exercises, or challenges, for you to do.
If you have a solution for one of the challenges, please post it below!

If you get stuck, be sure to ask about it here, or in a separate thread. Also, if you have a suggestion for a challenge, I’d like to hear those too!

I will try to sort the challenges a bit on difficulty.
These challenges are intended for C# and unity, but should be do-able in other languages as wel ðŸ˜€

Beginner:
– Change an object’s color when you click on it.
– Create an object spawner that spawns a prefab every second.
– Create a cube that you can ‘push’ away when you click on it. (physics)
– Controll a camera’s FOV with your scrollwheel.

–Â Create 2 threads. Make them log the numbers 0 – 99 to the console at the same time.
– Create a generic method. Log the datatype to the console of the variable that you feed it.
– Create an extension method with wich you can reset a transform’s position and rotation to 0, 0, 0.

Suggested:
–Â Create canon which fires rigidbody this. and precalulate the hit position of currentÂ cannon angle and the type of selectedÂ this.(mass,drag etc); (byÂ Eliman)
–Â Create physics bullet, that can penetrate obstacles, and loses its inertia depending of that obstacle width; (byÂ Eliman)
–Â Calculate point in space where two object will intersect depending on their movement trajectory (byÂ Eliman)

Good luck, and let me know of your progression!

• Liked by
• +1
on May 14, 2015

Beginner:
â€“ Change an objectâ€™s color when you click on it.

```using UnityEngine;
public class ColorChange : MonoBehaviour {
void OnMouseDown()
{
GetComponent<MeshRenderer>().material.color = new Color32((byte)Random.Range(0, 255), (byte)Random.Range(0, 255), (byte)Random.Range(0, 255), 255);
}
}
```

â€“ Create an object spawner that spawns a prefab every second.

```using UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour {
[SerializeField]
GameObject prefab;
void Start () {
StartCoroutine(Timer());
}
IEnumerator Timer()
{
while (true)
{
Instantiate(prefab, transform.position, transform.rotation);
yield return new WaitForSeconds(1);
}
}
}
```

â€“ Create a cube that you can â€˜pushâ€™ away when you click on it. (physics)

```using UnityEngine;
public class PushAway : MonoBehaviour {
Rigidbody rb;
void Awake () {
rb = GetComponent<Rigidbody>();
}
void OnMouseDown()
{
}
}
```

– Controll a cameraâ€™s FOV with your scrollwheel.

```using UnityEngine;
public class FovChange : MonoBehaviour
{
void Update()
{
if (Input.GetAxis("Mouse ScrollWheel") > 0) // IN
{
Camera.main.fov -= 5;
Camera.main.fov = Mathf.Clamp(Camera.main.fov, 10, 120);
}
if (Input.GetAxis("Mouse ScrollWheel") < 0) // OUT
{
Camera.main.fov += 5;
Camera.main.fov = Mathf.Clamp(Camera.main.fov, 10, 120);
}
}
}
```

on April 28, 2017

Is this JavaScript?

Guru
on April 28, 2017

No, C#.
Though JS looks a lot alike.

• Liked by
on May 14, 2015

â€“Â Create 2 threads. Make them log the numbers 0 â€“ 99 to the console at the same time.
(WARNING)since in my example i dont have any shared data i didnt syncronize them

```using UnityEngine;
using System;
public class Threading : MonoBehaviour {
void Start () {
t1.Start();
t2.Start();
}
public static void Printer()
{
for (int i = 0; i < 99; i++)
{
}
}
}
```

â€“ Create a generic method. Log the datatype to the console of the variable that you feed it.

```using UnityEngine;
using System.Collections;
public class TypePrinter : MonoBehaviour {
void Start()
{
LogMe<int>();
LogMe<float>();
LogMe<string>();
LogMe<decimal>();
LogMe<TypePrinter>();
LogMe<short>();
LogMe<ColorChange>();
}
void LogMe<T>()
{
Debug.Log(typeof(T).ToString());
}
}
```

â€“ Create an extension method with wich you can reset a transformâ€™s position and rotation to 0, 0, 0.
```using UnityEngine;
using System.Collections;
public static class GameObjectExtension {
public static void Reset(this GameObject go)
{
go.transform.position = Vector3.zero;
}
}
//test example
public class ResetPos : MonoBehaviour {
void Start()
{
gameObject.Reset();
}
}
```

Guru
on May 14, 2015

Nice work!
But I think you posted the wrong post for the generic method

Wise
on May 14, 2015

fixed. Can`t get rid of Â <em> </em> Â tag tho =(

• Liked by
on May 14, 2015

My suggestion to this topic : add more practical tasks, more Math oriented stuff,
for example:
-create canon which fires rigidbody this. and precalulate the hit position of currentÂ cannon angle and the type of selectedÂ this.(mass,drag etc);
-create physics bullet, that can penetrate obstacles, and loses its inertia depending of that obstacle width;
-calculate point in space where two object will intersect depending on their movement trajectory

Guru
on May 14, 2015

But my challenges are practical:
I try to use generics when I can.
I use a lot of threading.
And extensions can be very nice!

What I tried to do is getting people in contact with new stuff to learn.

Wise
on May 14, 2015

Can u give example where u use threading a lot? From my experience unity build their engine to avoid using threads, and stimulate programmers to avoid those stuff also.. maybe its just me who worked for two years withÂ microcontroller where no threading is supported ;C

Guru
on May 14, 2015

I’m working on a multiplayer game. So all networking logic is using two main seperate threads and some worker threads. My AI will probably work on a seperate thead too.

But no, if you don’t want to, you don’t have to use threads in Unity using coroutines. (Still you can get better performance if you do use threads)