Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jan 10, 2015 at 01:20 PM by tanoshimi for the following reason:

Duplicate Question

avatar image
1
Question by Yann_de_Couessin · Jan 10, 2015 at 01:19 PM · functions

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 !

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Yann_de_Couessin · Jan 10, 2015 at 04:37 PM 0
Share

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 :/

avatar image richyrich · Jan 11, 2015 at 02:30 AM 0
Share
 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

avatar image Yann_de_Couessin · Jan 11, 2015 at 11:22 AM 0
Share

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.

avatar image richyrich · Jan 11, 2015 at 05:01 PM 0
Share

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

avatar image Yann_de_Couessin · Jan 14, 2015 at 03:57 PM 1
Share

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 !

Show more comments

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges