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

accessing functions on other objects scripts.

Discussion in 'Scripting' started by chuckster, Apr 21, 2018.

  1. chuckster

    chuckster

    Joined:
    Jan 4, 2013
    Posts:
    28
    Forgive me for asking this as I see it ask alot, yet..I still haven't been able to find an answer that works for me. I've been at this all day. Like 8 hrs.
    I have my player ship which is instantiated with my level. he reacts to the enemy weapon which tells him to instantiate explosion and destroy objects.
    I have a game controll script on the game object it self (the entire game as a prefab) which is also instantiated. the control has two functions so far, reSpawn (re spawns player ship) or instantiate game over screen. both functions are called, but only one will work because of my if statement in relation to the remaining lives.
    If lives are greater than 0 , it respawns.
    My script idea worked great! But now I want my ship to just send the message to the game control script to execute those functions be for it self destructs. And you know -= a life!
    now listen, I've read the documentation on all of the get component, get object, broadcast message, haha I've tired it all. But the dam thing won't put the message through haha. I have a var with my main game object that the script is on, I I've tired in the start and awake function to do the gameObject.find, component.find, all that. And then in my on trigger, I state the path, but it's not happening.
    I know this is basic and pry really easy for one of you..how do I get the message through? How does player ship call that respawn or game over function on my game control?
    Maybe there's another way to manage this? Because next will be the points. I'll want to add points, take a live away. Add a life.
     
  2. chuckster

    chuckster

    Joined:
    Jan 4, 2013
    Posts:
    28
    I'm using Java btw
     
  3. chuckster

    chuckster

    Joined:
    Jan 4, 2013
    Posts:
    28
    Object reference is not set to instance of object. Haha but I dragged it in the var, on start I said Game_1= gameObject.find ("game_1") then I tested it with something simple like set active to false, but no go...
     
  4. chuckster

    chuckster

    Joined:
    Jan 4, 2013
    Posts:
    28
    I'm so mad...I swear I'm going to shoot an invisible collider from the ship as it destroyed, it will smash into the game controller and tell it to do the dam functions haha...grrrr the worst part is I've done this befor, I don't k ow why I'm having a hard time here..such a simple thing. .
     
  5. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,042
    If you are trying to find it in start, and you are getting a reference not set error, it means it probably didn't find it. If you are dragging the reference in to var in the inspector, then you don't need to do a find as well. Also, try posting your code, so we can see what you are actually doing. it may just be a simple mistake.
     
    chuckster likes this.
  6. chuckster

    chuckster

    Joined:
    Jan 4, 2013
    Posts:
    28
    Please forgive me for posting a pucture. My work pc is not on line and my phone won't connect to it. To send the code it self I'd have to take my SIM card out, and use a usb and a third computer to transfer lol...I'm having a he'll of a time ;;;p
     

    Attached Files:

  7. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,042
    Do you actually have an object named “Game_1)” ? That looks like a typo.
     
    chuckster likes this.
  8. chuckster

    chuckster

    Joined:
    Jan 4, 2013
    Posts:
    28
    Yeah it's not reading it at all because it won't even set active to false or any thing. I know the script I'm referencing works because I tested the functions . Null reference, not set to instance of object it says... oh I'm sure it's a simple mistake. I'm sorry to even have to ask...but I've really tried here lol
     
  9. chuckster

    chuckster

    Joined:
    Jan 4, 2013
    Posts:
    28
    Yes, like the whole game is a prefab, and that script is on it. well the coin screen, in Game_1 instantiates the levels
     
  10. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,042
    But does it actually have that parenthesis in the name? is it named Game_1 or Game_1)
     
  11. chuckster

    chuckster

    Joined:
    Jan 4, 2013
    Posts:
    28
    Here he is, Game_1 haha
     

    Attached Files:

  12. chuckster

    chuckster

    Joined:
    Jan 4, 2013
    Posts:
    28
    Oh! Let me check. sorry I'm behind lol, I barely get signal here.
     
  13. chuckster

    chuckster

    Joined:
    Jan 4, 2013
    Posts:
    28
    Omg....working like a champ now!....lololol. just forget you saw me...I'm going to die now.
    Thank you VERY much for your time sir. You probably saved my life. I won't hang my self in the shower now. ...my cats thank you
     
  14. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,042
    No worries, that stuff happens all the time, it’s easy overlook when you have been staring at the code for a long time.
     
  15. chuckster

    chuckster

    Joined:
    Jan 4, 2013
    Posts:
    28
    That was just something I tried and forgot to fully delete () lol. And it just didn't catch my eye next to that for some reason. Haha Lesson learned. Thank you again friend.