I’m working through Will’s EBook Unity Development Essentials and trying to transcode the UScript examples into C# scripts as I go so I thought it would be helpful to those who want to do same to ask for help on acomplishing this…
So here goes on my first problem the UScript is in the function Update() method in UScript:
function OnControllerColliderHit(hit: ControllerColliderHit){
if(hit.gameObject.tag == "outpostDoor" doorIsOpen == false){
openDoor();
then goes on to obviously declare a new method openDoor
but in C# it seems (Visual c# Express 2008) doesn’t like methods added in the void Update() code block so I am trying to figure out how to call with arguments the “OnControllerColliderHit” method from outside the void Update() code block by passing arguments to the method’s parameters and I suppose then I need to have a catcher variable to do something in the Update() method??
The error I am stuck on is:
“No overload method for ‘OnControllerColliderHit’ takes ‘0’ parameters”
Here’s where I am at so far:
using UnityEngine;
using System.Collections;
public class PlayerCollisions : MonoBehaviour
{
//Declare two private and two public variables ( public so show in Inspector )
private bool doorIsOpen = false;
private float doorTimer = 0.0f;
public AudioClip doorOpenSound;
public AudioClip doorCloseSound;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
OnControllerColliderHit();
}
// new method "OnControllerHit" no parameters but private local "hit" used in method, can't put in update so need to call it from Update()?
void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.gameObject.tag == "outpostDoor" doorIsOpen == false)
{
openDoor();
}
}
// new method for opening door called from "OnControllerHit"
void openDoor()
{
audio.PlayOneShot(doorOpenSound);
doorIsOpen = true;
GameObject myOutpost = GameObject.Find("outpost");
myOutpost.animation.Play("doorOpen");
}
}
TIA
I hope this thread may help with other c# newbies trying to work through Will’s EBook as I am trying to learn c# along the way?
Are yes I see that now just like Update() Start() OnApplicationQuit() etc etc these blocks of code are run when Unity events happen such as every frame, on script active, quitting the player etc etc
Yes it makes perfect sense now, seemed to get a bit mixed up trying to translate the UScript, somehow I thought it was a custom method never mind all good just learning C# by translation.
For those trying to translate the UnityScript examples into C# here’s what I came up with by the end of Chapter 4:
using UnityEngine;
using System.Collections;
public class PlayerCollisions : MonoBehaviour
{
//Declare two public variables for sound clips visible in Unity Inspector to assist assigning door sounds for open / shut in editor
public AudioClip doorOpenSound;
public AudioClip doorCloseSound;
// Declare two variables; for a state check 'doorIsOpen' (true or false); and 'doorTimer'
//to be used to shut the door after an amount of value + Time.deltatime (to keep synced to frame rate) becomes more than 3 secs
float doorTimer = 0;
bool doorIsOpen = false;
// Use this for initialization
void Start()
{
// Nothing to do here yet
}
// Update is called once per frame
void Update()
{
// Using Raycast technique as to opposed collider checking 'OnColliderHit()' technique that is described in Chapter 4
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit, 5))
{
if (hit.collider.gameObject.tag == "outpostDoor" doorIsOpen == false)
{
Door(doorOpenSound, true, "dooropen");
}
}
if (doorIsOpen)
{
doorTimer += Time.deltaTime;
if (doorTimer > 3)
{
Door(doorCloseSound, false, "doorshut");
doorTimer = 0;
}
}
}
/*original if statement calling the method 'shutDoor' for shutting the door after 3 secs,
* now we use new method 'Door' passing arguments to the method 'Door(AudioClip aClip, bool openCheck, string animName)'
* to be received as parameters for that method named 'Door'
*
if (doorTimer > 3)
{
shutDoor();
doorTimer = 0;
}
}
*/
/* TEMP COMMENTED OUT WHILST USING RAYCASTING METHOD IN Update() METHOD INSTEAD
void OnControllerColliderHit (ControllerColliderHit hit)
{
if (hit.gameObject.tag == "outpostDoor" doorIsOpen == false)
openDoor();
}
*/
/* TEMP INACTIVE AS WE REPLACE THE openDoor() METHOD WITH ONE THAT PASSES ARGUMENTS TO PARAMETERS IN THE METHOD 'Door'
void openDoor()
{
audio.PlayOneShot(doorOpenSound);
doorIsOpen = true;
GameObject myOutpost = GameObject.Find("outpost");
myOutpost.animation.Play("dooropen");
}
*/
// TEMP INACTIVE AS WE REPLACE THE shutDoor() METHOD WITH ONE THAT PASSES ARGUMENTS TO PARAMETERS IN THE METHOD 'Door'
/*void shutDoor()
{
audio.PlayOneShot(doorCloseSound);
doorIsOpen = false;
GameObject myOutpost = GameObject.Find("outpost");
myOutpost.animation.Play("doorshut");
}
*/
void Door(AudioClip aClip, bool openCheck, string animName)
{
audio.PlayOneShot(aClip);
doorIsOpen = openCheck;
GameObject myOutpost = GameObject.Find("outpost");
myOutpost.animation.Play(animName);
}
}
Hi all, just checking book related threads to bring you news that my book on Unity is finally finished, and is heading to the printers this week, designed for beginners to get them kickstarted you can find out about it here…