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
0
Question by Slaghton · Feb 02, 2014 at 09:30 PM · gameobjectprefabvariablenullreferenceexceptionother

Access variable from script on other gameobject.

I'm trying to access a variable from another gameobject to reference it but i'm not having any luck since i only know how to do it if the components are all on the same object.

Here's a simplified version of the 2 scripts without the extra functions.

Enemy_Attacked.js on Enemy

 var MeleeInstance;
  
  
 function Start () {
 Meleeinstance = GetComponent(Melee);   
 }
  
 function Update () {
 }
  
     function OnTriggerEnter (col : Collider)
     {
        
         if(MeleeInstance == true)  
         if(col.tag == "Weapon")
         {
         MeleeInstance.swordReady = false;     
         }
     }


Melee.js On player Code:

     public var Enemy_Attacked_Instance;
      var swordReady : boolean = false;
    
     function Start () {
      Enemy_Attacked_Instance = Enemy_Attacked;
  
     }
      
     function Update ()
     {
         if (Input.GetButtonDown("Fire1"))
         {
             swordReady = true;
             StartCoroutine("DisableSwordReady");
         }
     }
    
     function DisableSwordReady()
     {       
         yield WaitForSeconds(1);
         swordReady = false;
 }

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

· Add your reply
  • Sort: 
avatar image
0

Answer by getyour411 · Feb 02, 2014 at 09:32 PM

Try

 MeleeInstance = GameObject.FindObjectWithTag("Player").GetComponent.<Melee>();

or whatever the JS equiv is

This site has great tuts on getting components from script a / script b http://unitygems.com/script-interaction-tutorial-getcomponent-unityscript/

Comment
Add comment · Show 2 · 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 Slaghton · Feb 02, 2014 at 09:38 PM 0
Share

Thanks for the reply! I'll review this info asap and get back to you. I've seen that code before and it does seem like its the way to go. (I've read the listed tutorial and its what I have working for the rest of the scripts that's not posted but doesn't work with calling a component from another object :(

avatar image Slaghton · Feb 02, 2014 at 10:05 PM 0
Share

(actually i may have a fix. i'll apply it in the morning and see what happens. Think I just need to do abit of switching around)

Hm, i'm getting:

Assets/Scripts/Enemy_Attacked.js(7,69): BCE0138: 'quack' is not a generic definition.

Seems this guy had the same problem, but i'm close. Just have to restructure this. hm

http://forum.unity3d.com/threads/87870-Static-vars-and-multiple-enemies

Well i got halfway into fixing this last part but in melee.js I can't change swordready to either true or false with a swordReady = true/false. On the other hand, in Enemy_Attacked I can do melee.swordReady = true/false and it will change so idk.

Edit Been working on this for about 15 hours straight lol. Will continue after I catch some sleep.

Example of current code. $$anonymous$$elee.js attached to player

     var myEnemyObject : GameObject;
       var swordReady : boolean = false;
    
     function Start () {
     myEnemyObject = GameObject.Find("Enemy_Attacked");;
      
     }
      
     function Update ()
     {
         if (Input.GetButtonDown("Fire1"))
         {
             swordReady = true;
             networkView.RPC("SyncAttackAnimation", RPC$$anonymous$$ode.All);
             StartCoroutine("DisableSwordReady");
         }
     }
    
     function DisableSwordReady()
     {
    
         yield WaitForSeconds(1);
         swordReady = false;
 }       
     @RPC
     function SyncAttackAnimation(){
             animation.Play("Attack");
             animation["Attack"].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Once;
             }



Enemy_Attacked attached to enemy

 var enemyLogicInstance;
 var myPlayerObject : GameObject;
  
  
 function Start () {
 enemyLogicInstance = GetComponent(EnemyLogic);
 myPlayerObject = GameObject.Find("$$anonymous$$elee");
  
 }
  
  
 function Update () {
  
 }
  
     function OnTriggerEnter (col : Collider)
    
     {
         var melee = myPlayerObject.GetComponent($$anonymous$$elee);
         if(col.tag == "Weapon")
         {
         if(melee.swordReady == true)
         {
         melee.swordReady = false;
         networkView.RPC("RemoveHealth", RPC$$anonymous$$ode.All);
         }
     }
 }
  
  
     @RPC
     function RemoveHealth(){
     enemyLogicInstance.Enemy_Health -= 25;
     print(enemyLogicInstance.Enemy_Health);
     }

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

18 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

Related Questions

How do I assign a prefab reference to a private variable? 1 Answer

Setting a game object from a list to be the active character that gets instantiated 1 Answer

What is the type of my prefab? 2 Answers

Set Rigidbody variable to Prefab using code 1 Answer

[Solved]Instantiating prefab from a script and destroy it from another one 2 Answers


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