Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by PrincessPiranha · Sep 22, 2011 at 08:26 AM · getcomponentvarotherin

Can't get var from other script.

I have two javascript scripts:

Force.js:

 var power : float = 50.0;
 var Mage : GameObject;
 
 function Update () {
     var other : PunchWall;
     other = Mage.GetComponent("PunchWall");
     Debug.Log(other);
     /*if(other.punchOK==1){
         rigidbody.AddForce(Vector3(power,0,0));
     }*/
 } 

PunchWall.js:

 var startpunch : float;
 var punchOK : int = 0;
 
 function Update () {
 
     if(Input.GetButtonDown("Jump")) {
         startpunch = 35;
         punchOK = 1;
     }
     
     if(startpunch>0) {
         animation.Play("punch");
         startpunch--;
     }
 }
 
 function OnTriggerEnter(theCollision : Collider){
     if(theCollision.gameObject.name == "Hitbox"){
         if (punchOK == 1) {
         }
     }
 }

And I want to use the variable 'punchOK', which is stated in my 'PunchWall' script and is linked to my player named Mage. (Force is linked to my Brick, which is supposed to fly away after it gets punched.

I put a Debug in the Update and it gives 'Null'. So it doesn't even fill my var other?

Comment
Add comment · Show 6
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 PrincessPiranha · Sep 22, 2011 at 08:58 AM 0
Share

Hmmm, Debug still gives Null.

avatar image BiG · Sep 22, 2011 at 09:12 AM 0
Share

Ok, maybe "other=GameObject.Find("$$anonymous$$age").GetComponent(PunchWall).punchO$$anonymous$$;" However, I'm still not sure about that, because I don't have Unity here, in this moment! Hope to help you, this time...

avatar image PrincessPiranha · Sep 22, 2011 at 09:31 AM 0
Share

That's okay, I appreciate any help!

It does give an other error now though: "InvalidCastException: Cannot cast from source type to destination type."

avatar image BiG · Sep 22, 2011 at 09:39 AM 0
Share

Ah, yes: you are declaring other as "PunchWall",in Force.js, but the istruction that I've posted above returns an integer, now. Remove "var other : PunchWall" and declare "other" directly into the istruction above (var other=GameObject.Find....); or declare "other" as int.

avatar image PrincessPiranha · Sep 22, 2011 at 09:45 AM 0
Share

That worked!!! Thank you so much! :)

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Sep 22, 2011 at 10:00 AM

You should do the following to get the punchOk value:

var power : float = 50.0;

function Update () { var other : PunchWall; other = GameObject.Find("Mage").GetComponent(PunchWall); Debug.Log(other); if (other.punchOK==1){ rigidbody.AddForce(Vector3(power,0,0)); } } But it seems the whole thing is wrong: the better way to do that would be to apply the force in OnTriggerEnter or OnCollisionEnter (depends on the brick's collider isTrigger flag).

Comment
Add comment · Show 1 · 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 PrincessPiranha · Sep 22, 2011 at 11:01 AM 0
Share

No I got it working already, thanks to BiG! :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How do I add/subtract variables between two different JavaScripts? 1 Answer

is this correct way to change var from other script? 1 Answer

gameObject.GetComponent("Script").enabled = true not working 5 Answers

Get Component from Instantiated Prefab 1 Answer

script GetComponent, nullReference error 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