Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Need help referencing another script and gameobject without drag and drop method

Discussion in 'Scripting' started by static2601, May 9, 2014.

  1. static2601

    static2601

    Joined:
    May 9, 2014
    Posts:
    6
    Hi, im fairly new to Unity and C#, but have done enough of it in the past couple months to understand how to write it, but im having trouble. I want to call a function from another script without making it public and drag dropping it from the editor. Ive had it setup as public ScriptName Name, then drag drop an object with the script. I would like to avoid having to set all these from within the editor, so right now, Im using this:
    Code (csharp):
    1.  
    2. public class GUIMenuButtonsScript : MonoBehaviour {
    3.    
    4.     private CameraFollowPlayer script1;
    5.     private ShipMove ship;
    6.  
    7.     void Start()
    8.     {
    9.         script1 = GetComponent<CameraFollowPlayer>();
    10.         ship = GetComponent<ShipMove>();
    11.         }
    12.  
    So when I type something like ship.Firelaser(); it autocompletes it. but in game, it throws an error when trying to do it.
    Code (csharp):
    1.  
    2. NullReferenceException: Object reference not set to an instance of an object
    3. GUIMenuButtonsScript.OnTouchDown () (at Assets/GUIMenuButtonsScript.cs:59)
    4. UnityEngine.GameObject:SendMessage(String, Object, SendMessageOptions)
    5. TouchInput:Update() (at Assets/Scripts/TouchInput.cs:62)
    6.  
    Also, How would I write this without having to drag drop it in editor?
    This is my current working code to use with drag and drop:
    Code (csharp):
    1.  
    2. public GameObject goFixed;
    3.  
    4. public void CameraToggle ()
    5.     {
    6.         if(goFixed.activeSelf)
    7.         {
    8.             goFixed.SetActive(false);
    9.         }
    10.         else
    11.         {
    12.             goFixed.SetActive(true);
    13.         }
    14.  
    the reason I want to do it another way is because I use the same script for all the menu pages and I have to redo the drag drop for each one and getting to be a mess. Ive been rewriting everything to better optimize and organize it, so im not losing time chasing down problems.
    Thanks

    }
     
  2. El_Guig

    El_Guig

    Joined:
    May 13, 2013
    Posts:
    40
    What you showed in your first part of code is right, it autocompletes because ShipMove is a known type so Firelaser() is a known method from that script. However, when you initialise it (ship = GetComponent<ShipMove>();), it doesn't seem to be found, as referred to by the null reference exception. Is the ShipMove script attached to the same GameObject as the GUIMenuButtonsScript ? If not, then this is why you can't access it directly and need to either put them on the same GameObject or have something along the lines of:
    Code (csharp):
    1.  
    2. ship = GameObject.Find("MyObjectWithShipMoveAttached").GetComponent<ShipMove>();
    3.  
    Same goes for the other script you declare.
     
  3. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    Just to add something :
    If you use a tag on your item, it will be faster to access it than looking through all the objects for a specific name.
    Using
    Code (csharp):
    1.  ship = GameObject.FindGameObjectWithTag("MyObjectWithShipMoveAttached").GetComponent<ShipMove>();
     
  4. static2601

    static2601

    Joined:
    May 9, 2014
    Posts:
    6
    Ship is its own script on its own object, not a child of guimenubuttons gameobject. But makes sense now that you said it, It didnt dawn on me that GetComponent is only getting the component of the parent. Ill give this a try thanks.
     
  5. static2601

    static2601

    Joined:
    May 9, 2014
    Posts:
    6
    Didnt know that, I have tags for other objects but thought it would be easier and pretty much the same just to search by name. I still have a lot to learn lol. Thanks