- Home /
Error accessing var from other scripts within project via GetComponent
Hello!
I am following a set of tutorials on working with unity, but I seem to have hit a roadblock, Here is the scenario: I have a player script and a enemy script. The objective is to create a public variable in the player script to hold the number of lives left. In the enemy script, within an OnTriggerEnter function, the collision should cause a player life to drop by 1, but once I do it all, I get an error in unity stating that:
Assets/folderScripts/scipt enemy(37,61): BCE0019: 'lives' is not a member of 'UnityEngine.Component'.
Here is my variable declaration in the player script :
 var lives            :    int        =    3;
and here is my OnTriggerEnter function in my enemy script:
 function OnTriggerEnter ( other : Collider)
 {
     if (other.gameObject.tag == "Player")
         {
             other.GetComponent("script player").lives -= 1;
         }
 }
It's worth mentioning that the gameObject associated with the player script is tagged as "Player"
Thanks for your help in advance.
Answer by SubatomicHero · May 13, 2013 at 08:01 AM
Just for neatness and showing a clear trail of thought, you could try this:
 if (other.gameObject.tag == "Player")
 {
     var player : ScriptName = other.GetComponent(Player);
     player.lives -= 1;
 }
its better to try and use types rather thans strings in GetComponent. For example if a script is called Player its better to do GetComponent(Player) rather than GetComponent("Player");
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                