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 /
avatar image
-1
Question by Banana13 · Feb 18, 2014 at 04:16 PM · c#javascriptgetcomponentboolean

Can someone translate C# to Javascript?

Can someone please translate this into Javascript:

 if (GameObject.Find("cube").GetComponent<bonus>().bonuson)

Cube is the Object with the Script bonus.js and this Script needs to check if bonuson is true.

Thanks.

Comment
Add comment · Show 3
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 Banana13 · Feb 19, 2014 at 07:24 AM 0
Share

$$anonymous$$y script is still not working. :(

I have a Player with the Script slo$$anonymous$$o and a cube with the Script bonus. And if the Player gets the cube with the Script bonus, he can slow down time by pressing the button "slomo".

So how can I make the Script slo$$anonymous$$o check if the Player got the cube?

The Script on the Player (slo$$anonymous$$o):

     var currentBulletTimer : float = 0;
     var bulletTimeAllowed : float = 3.0; // seconds
     
     function Update () {
     if (GameObject.Find("cube").GetComponent(bonus).jep)
     if (Input.GetButtonDown ("slomo")) {
     if (Time.timeScale == 1.0)
     Time.timeScale = 0.3;
     else
     Time.timeScale = 1.0;
      
     //Time.fixedDeltaTime = 0.02 * Time.timeScale;
     }
      
     if (Time.timeScale == 0.3) {
     currentBulletTimer += Time.deltaTime;
     }
      
     if ( currentBulletTimer > bulletTimeAllowed ) {
     currentBulletTimer = 0;
     Time.timeScale = 1.0;
     }
     }

The Script on cube (bonus)

 var jep : boolean = false;
 
     function OnTriggerEnter(other : Collider)
     {
      if (other.gameObject.tag == "Player")
     jep = true;
     Destroy(gameObject);
     
     }

Thanks for helping.

avatar image robertbu · Feb 19, 2014 at 07:36 AM 0
Share

This is really a new question. You have a problem here. In your bonus script, you set 'jep = true', but then you immediately destroy the cube and therefore the script that contains 'jep' is also destroyed. That means that your GameObject.Find() in slo$$anonymous$$o will not find this specific cube. You have a many choices and here are a few.

First, you could not destroy the cube, ins$$anonymous$$d just hide it. Replace line 7 in the 'bonus' script with:

 renderer.enabled = false;
 collider.enabled = false;

Another solution would be for the cube to set a variable in the 'slo$$anonymous$$o' script that says he got the cube. At the top of the 'slo$$anonymous$$o' script have:

 var gotCube = false;

Then in the cube code:

 if (other.gameObject.tag == "Player") {
     sm = other.gameObject.GetComponent(slo$$anonymous$$o);
     sm.gotCube = true;
 }

Another solution (probably the best solution) if your player does not have a character controller, is to have an OnTriggerEnter() on the player, and set the 'gotCube' variable inside that function.

A couple of notes:

  • Line 7 of 'bonus' gets execute regardless of whether the tag is 'Player'. So if something else besides the player hits the cube, it will be Destoryed.

  • Line 5 of slo$$anonymous$$o find a instance of an object named 'cube'. If you have multiple 'cube' objects, it will not necessarily find the one that had the trigger fire.

avatar image Banana13 · Feb 19, 2014 at 09:09 AM 0
Share

thanks its working perfekt now :)

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Feb 18, 2014 at 04:17 PM

Almost the same:

  if (GameObject.Find("cube").GetComponent(bonus).bonuson)

http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html

Comment
Add comment · 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
1

Answer by ragnaros100 · Feb 19, 2014 at 07:28 AM

In general it isnt the usual code of cunduct to post "Please do this for me"-questions around here. I can see that you are new, but please excercise more caution in the future.

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Convert this line of javascript to C# (easy) 1 Answer

Change a Variable with another script not working (C#) 4 Answers

Can a script be loaded to a next scene? 3 Answers

What will happen if I GetComponent in this way? (Example) 1 Answer

Translate code from C# to Javascript 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