The following script can be attached to Unity’s camera & will generate the first 20 values of the Fibonacci sequence.

// 	ADD COMMENTS FOR EACH LINE EXPLAINING WHAT THE CODE DOES; REFER TO SPECIFIC VARIABLE & METHOD NAMES

// ATTACH SCRIPT TO MAIN CAMERA

// 	EXERCISE 3: IN THE FIBONACCI SEQUENCE, YOU DETERMINE A NUMBER BY ADDING THE TWO PREVIOUS NUMBERS TOGETHER. 
//  THE SEQUENCE STARTS WITH 0, 1, 1, 2, 3, 5... WRITE A SCRIPT THAT DETERMINES THE FIRST 20 PLACES OF 
//  THE FIBONACCI SEQUENCE AND OUTPUTS THEM TO THE CONSOLE.


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

public class Exercise3 : MonoBehaviour {

	static void ClearConsole () {			// Create a new method, ClearConsole() which clears all existing messages in the console. Very handy.
         Debug.ClearDeveloperConsole ();	// Accesses the Debug class, which has several useful methods for-- you guessed it, debuging your program. 
     }

	public int Fibonacci (int n)			// Create a new method Fibonacci() for handling the main math operation. As shown inside the parenthesis "(int n)", 
											// this method requires an input of an integer 'n' to properly work. This function outputs an integer, which is publicly accessible outside of this script.
	{
		int a = 0;							// Create a new integer variable called 'a' and assign it a value of 0. 
		int b = 1;							// Create a new integer variable called 'b' and assign it a value of 1. 

		for (int i = 0; i < n; i++) {		// start a For() loop, with these parameters: create an integer variable 'i' to count the number of times this loop repeats, 
											// continue repeating this loop until the counter value of 'i' is less than 'n.' Finally, after executing each loop, the counter value of 'i' increases by 1.    

			int temp = a;						// Here's where it gets tricky-- INSIDE THE LOOP- Create a new integer variable called 'temp' and assign it the current value of the previously-created 'a' variable. 

			a = b;								// Assign 'a' to the current value of the 'b' variable.
			b = temp + b;						// Add the current value of 'temp' to the current value of 'b'.
		}
		return a;							// End of loop. Once the For loop aboe repeats its specified number of times, the final value of 'a' is exported from this method. 
											// Inside this Fibonacci() loop the values of 'a' and 'b' are repeatedly added together, using the 'temp' 
											// variable to pass along the subtotals for the next round of addition.

	}										// End of Fibonacci() method. 
											

	// Use this for initialization
	void Start ()							// Start() executes as soon as the game starts.
	{
		for (int i = 0; i < 20; i++) {		// Create another For loop to contain the 'inner' loop above. Starts by defining a counting integer value, 'i,' 
											// repeating operation 20 times, adding 1 to 'i' after each loop.
			
		print (Fibonacci(i));				// Print a message in the Console showing the current output from the Fibonacci() method, 'a' as specified on line 34 above. Repeat this message 20 times.
		}

	}
		
	
	
	// Update is called once per frame
	void Update () {
		
	}
}

 

Leave a Reply