- Home /
Duplicate Question
Can't use GetComponent (Calling JS in C#), and don't know why
Hello everyone,
I'm having troubles to run an autowalk function with my VR project. Here's the deal : I'd like to make a VR camera with an autowalk function (like in the Tuscany demo) : to walk, you just have to look your feet and an icon will turn from red to green, activating the autowalk.
I used the Google cardboard SDK for the camera asset, and the Durovis Dive SDK for the autowalk function.
But to toogle the autowalk int value, I have to call in my C# forward.cs an int function autowalk from a JS script.
The script execution order is set to launch the JS first, but it doesn't work (because i'm pretty bad, I'm a videomaker, not a developer :D
Here's my forward.cs :
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Collider))]
public class avance : MonoBehaviour {
public float speed=1f;
private CardboardHead head;
void Start() {
head = Camera.main.GetComponent<StereoController>().Head;
}
void Update() {
RaycastHit hit;
bool isLookedAt = GetComponent<Collider>().Raycast(head.Gaze, out hit, Mathf.Infinity);
GetComponent<Renderer>().material.color = isLookedAt ? Color.green : Color.red;
if (Cardboard.SDK.CardboardTriggered && isLookedAt) {
// Action à déclencher
GetComponent<DiveFPSController>.toggle_autowalk();
}
}
}
et the JS autowalk public function, from DiveFPSController.js
function toggle_autowalk(){
if (autowalk==0)autowalk=1;
else autowalk=0;
}
I searched so many hours on forums to call a JS function properly, but I don't understand what's missing. Thanks in advance !
Answer by tanoshimi · Jan 10, 2015 at 01:20 PM
Move your is script into the Standard Assets folder so it is compiled before the C# script.
Hi, thanks for your answer, but I already done that, I thinks the problem is how I call that JS function :
GetComponent<DiveFPSController>.toggle_autowalk();
I have this error returned :
Assets/avance.cs(34,9): error CS0119: Expression denotes a method group', where a
variable', value' or
type' was expected
I suppose I just take it the wrong way :/
GetComponent<DiveFPSController> ( ).toggle_autowalk();
The damn text editor won't let me write it normally, but basically you might need the extra parenthesis immediately preceeding '.toggle
Hi Richyrich, I tried you suggestion, and I can launch the scene now, but when I clic on the feets, the scene stops and I got :
NullReferenceException: Object reference not set to an instance of an object avance.Update () (at Assets/avance.cs:34)
In monodevelop, I got on DiveFPSController a red color indicating
the error: CS0103: The name `DiveFPSController' does not exist in the current context
Is my function not found ?
Thanks guys.
Have you made sure that the type was attached in the first place? You could click on your object and check in the inspector during runtime to make sure
Oh ! Right I understand. It works with DiveFPSController on my component now, but the problem is that this script must be on the parent, the camera, not the feet icon :D
So I used
transform.parent.GetComponent<DiveFPSController> ( ).toggle_autowalk();
and now it works. Thanks a lot to everybody !
Follow this Question
Related Questions
how can I add the delay between different function calls in C#.. 3 Answers
Mixamo Animation and Animation Editor 1 Answer
when is pysics.raycast used, if it is called in Update() 1 Answer
Verification Exception with Custom JS Function 0 Answers
How to make an Object react if a key is hit within a trigger? 1 Answer