Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Script won't access other script's functions c#

Discussion in 'Scripting' started by LizThrelfo, Apr 23, 2012.

  1. LizThrelfo

    LizThrelfo

    Joined:
    Apr 20, 2012
    Posts:
    24
    So I've recently started a game way more ambitious than anything i've done previously and i'm unfortunately stumbling very early on.

    I've created two classes, the first is my player and raises and lowers with the left mouse button and the second is an object that is activated when clicked with the left mouse.

    Where I'm stuck is getting the two to communicate properly i.e access functions and variables from the other and vise versa. I'm using the method I've seen across the forums where you drag and drop your target object in the editor (described here http://answers.unity3d.com/questions/10857/how-can-i-access-other-scripts-and-their-functions.html). I've tried other ways such as using tags or object names but i'm having more trouble with those.

    At the moment the first script (the player) is able to call the function Hello1 in the second (the object).
    when I attempt to call Hello in the first from the second nothing is happening. Eventually i would like to create a condition which will prevent clicks on objects from affecting the player's movement but this is a hurdle i cant figure out.


    First Script
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class S_HighControl : MonoBehaviour {
    5.  
    6.     tk2dAnimatedSprite anim;
    7.     float balloonX = 0;
    8.     public float HighUp = 0.02f;
    9.     public float HighDown = 0.01f;
    10.  
    11.     public S_Multitasking Bush;
    12.    
    13.     //attempt at bringing in a bush
    14.    
    15.     // Use this for initialization
    16.     void Start () {
    17.         //rigidbody.freezeRotation = true;
    18.         anim = GetComponent<tk2dAnimatedSprite>();
    19.         //anim.CurrentClip.name = ("High Idle Full Health");
    20.         anim.Play("High Idle Full Health");
    21.  
    22.         Bush.Hello1();
    23.     }
    24.  
    25.     bool accelerating = false;
    26.    
    27.     // Update is called once per frame
    28.     void Update ()
    29.     {
    30.        
    31.         //this makes sure he doesnt go off screen
    32.         ScreenLimits();
    33.        
    34.  
    35.         //checking if the left mouse button is pressed or held down, which will see high accelerate
    36.             if (Input.GetMouseButton(0))
    37.             {
    38.  
    39.                     //this line raises High by the amount HighUp
    40.                     transform.position = new Vector3(transform.position.x, transform.position.y + HighUp, transform.position.z);
    41.  
    42.                     //this statement checks if an animation is playing, if it isnt his acceleration it will play
    43.                     //if the current clip playing is his acceleration then it will skip and allow it to continue
    44.                     if (!anim.isPlaying() || anim.CurrentClip.name != "High Acceleration 1")
    45.                     {
    46.  
    47.                         anim.Play("High Acceleration 1");
    48.                     }
    49.  
    50.                     //this makes sure he doesnt go off screen
    51.                     ScreenLimits();
    52.  
    53.                     //accelerating = true;
    54.                
    55.             }
    56.            
    57.             else
    58.             {
    59.                
    60.                 ScreenLimits();
    61.                 transform.position = new Vector3(transform.position.x + balloonX, transform.position.y - HighDown, transform.position.z);
    62.                
    63.                 if (anim.isPlaying()|| anim.CurrentClip.name != "High Idle Full Health")
    64.                 {
    65.                    
    66.                     anim.Play("High Idle Full Health");
    67.                    
    68.                     //accelerating = false;
    69.                 }
    70.             }
    71.              
    72.         }
    73.  
    74.     void ScreenLimits()
    75.     {
    76.         //prevent movement off screen in the y (up)
    77.         if (transform.position.y > 4.9f)
    78.         {
    79.             transform.position = new Vector3(transform.position.x ,4.9f, transform.position.z);
    80.             //LivesCounting();
    81.         }
    82.  
    83.         //prevent movement off the screen in the y (down)
    84.         if (transform.position.y < -2.5f)
    85.         {
    86.             transform.position = new Vector3(transform.position.x , -2.5f, transform.position.z);
    87.             //LivesCount = LivesCount - 1;
    88.             //LivesCounting();
    89.         }
    90.     }
    91.        
    92.     public void Hello()
    93.     {
    94.         print("Hello from High");
    95.     }
    96. }
    Second Script
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class S_Multitasking : MonoBehaviour {
    5.  
    6.  
    7.     public bool MultiTasking = false;
    8.     public S_HighControl High;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.        
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.                //if (MultiTasking == true)
    18.             High.Hello();
    19.     }
    20.  
    21.     public void Hello1()
    22.     {
    23.         //MultiTasking = true;
    24.         print("hello from multitasking");
    25.  
    26.     }
    27.  
    28.    
    29.     void OnMouseUp()
    30.     {
    31.         //MultiTasking = true;
    32.         print("clicked on object");      
    33.     }
    34.    
    35. }
    I hope all that makes sense :S and any insight or suggestions will be greatly appreciated.
     
  2. ahmetDP_1

    ahmetDP_1

    Joined:
    Sep 23, 2010
    Posts:
    113
    where are your scripts located in the Assets? Are they in the same folder?

    There is a compilation order for scripts in different folder, this could be the issue here.
     
  3. LizThrelfo

    LizThrelfo

    Joined:
    Apr 20, 2012
    Posts:
    24
    They are in the same folder one called 'Scripts' I've created within my project.

    I forgot to mention that i'm using 2dToolkit if that provides any more insight?
     
  4. Tobias J.

    Tobias J.

    Joined:
    Feb 21, 2012
    Posts:
    423
    I'm surprised you can call Bush.Hello1() at all, since I can't see that it's being initialized.

    Have you seen this?
     
  5. LizThrelfo

    LizThrelfo

    Joined:
    Apr 20, 2012
    Posts:
    24
    Do you mean line 21 of the second script? Bush is the new variable I've made in the first script using the second script as its type (at least thats how i understand it)

    The scripting reference is actually what set me on this direction, I've tried as many of the examples it gives as i could but I haven't been able to get any further than the above code
     
  6. Tobias J.

    Tobias J.

    Joined:
    Feb 21, 2012
    Posts:
    423
    No. Line 22 in the first script.

    You never do a Bush.GetComponent<S_Multitasking>(), in which case I'd expect you to get a null reference when you try to do stuff with it.
     
  7. LizThrelfo

    LizThrelfo

    Joined:
    Apr 20, 2012
    Posts:
    24
    Hmm I see your point there, my thinking is that by making the variable public then dragging the target object on (in the manner the link i mentioned suggests) I'm removing the need for that line?
     
  8. Tobias J.

    Tobias J.

    Joined:
    Feb 21, 2012
    Posts:
    423
    Right you are. You said so in the first post as well. My bad.

    You did the same for the second script (drag S_HighControl script to high variable), of course?

    If so, I'm drawing a blank. :-(
     
  9. LizThrelfo

    LizThrelfo

    Joined:
    Apr 20, 2012
    Posts:
    24
    No worries thanks for your help anyways. Yep as far as I can tell I have done exactly the same thing :/

    Gosh this is annoying I've spent my whole day on this; I'm at the stage now that my first born child will be named after whomever finds what I'm doing wrong haha
     
  10. Leiter Jakab

    Leiter Jakab

    Joined:
    Sep 25, 2010
    Posts:
    53
    It works fine. Bush.Hello1 gets called from the Start() function of S_HighControl, then High.Hello() gets called every frame from the Update() function of S_Multitasking.
     
  11. LizThrelfo

    LizThrelfo

    Joined:
    Apr 20, 2012
    Posts:
    24
    If that's true why isn't it printing the message from Hello()? Am I doing something else that's overriding that?

    EDIT: I may be the dumbest person on the planet, I unchecked the script by mistake >.< so now it will call in the update and the start. The onmouseup() seems to be the actual problem which was working but now evidently isn't.

    Thanks heaps for your help everyone and sorry I'm such a noob
     
    Last edited: Apr 24, 2012
  12. Leiter Jakab

    Leiter Jakab

    Joined:
    Sep 25, 2010
    Posts:
    53
    I only commented out the tk2dAnimatedSprite anim releated lines from the S_HighControl class. So as far as I can tell there's nothing in these classes that would prevent it from being called.