Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need help filling in code C# using UnityEngine; using System.Collections; public

ID: 3676826 • Letter: N

Question

Need help filling in code C#

using UnityEngine;

using System.Collections;

public class WeightScript : MonoBehaviour

{

// The weight of this item (by itself)

public float weight;

// Whether or not this is a container/crate

public bool isContainer;

// Recursively calculate the weight of the object public float CalculateWeight() {

// If this is NOT a container, just return your weight

// Otherwise

// 1) Print out your name "this.name"

// 2) create a "totalWeight" variable and assign it your weight

// 3) Loop through all the (child) transforms for this object // 3.1) call the child's CalculateWeight() method and add it to the totalWeight // 3.2) It's a good idea to print out the tag of this object as well as the weight

// 4) return the totalWeight // Remove the line below - it's just there to compile return this.weight; }

}

Explanation / Answer

using UnityEngine;

using System.Collections;

public class WeightScript : MonoBehaviour

{

// The weight of this item (by itself)

public float weight;

// Whether or not this is a container/crate

public bool isContainer;

public float CalculateWeight() {

//if not a container returning weight

if(isContainer==false)

return weight;

else {

Console.WriteLine(this.name);

float totalWeight = weight;

// loop through all child transforms

  foreach(Transform child in transform)

{

//call CalculateWeight method

int returnValue = CalculateWeight(child.childObject);

//adding weight to the total weight

totalWeight = returnValue + totalWeight;

Console.WriteLine(child.childObject.tag);

Console.WriteLine(weight);

}

}

return totalWeight;

}

  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote