Search Unity

having problems access a child script, need help.

Discussion in 'Scripting' started by tim76, Jun 25, 2017.

  1. tim76

    tim76

    Joined:
    Nov 11, 2014
    Posts:
    20
    Yes, I am a newbie and probably asking a dumb question that been asked a 1000 times. I tried to search the forum but got so many hits that wasn't what looking for. So I am asking a dumb question, Anyway...

    I have Cube and under the cube I have Canvas as a child. I am using the canvas for a menu for that Cube.
    I created Script called 'closeme' under the canvas to open and close the menu with 'hideme' boolean. The Close works perfect but having trouble getting to open again.

    I have script under the cube to rightclick to open the canvas, and this is what I got and it's not working...

    Code:
    function Update () {

    if(Input.GetMouseButtonDown(1)) {
    var ChildComp = GetComponentInChildren(closeme);
    ChildComp.hideme = false;

    }

    }


    Error:

    NullReferenceException: Object reference not set to an instance of an object
    RightClickMenu.Update () (at Assets/Jscript/RightClickMenu.js:11)


    I tried messing with for hours, and now asking you guys, can anybody tell me what I am doing wrong.
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, if you are new to coding and wanting to learn it and Unity, I'd recommend trying C# vs UnityScript. That's a "soft" recommendation, for lack of a better term.. You can use either, but if you're new I think you'd get a lot more out of learning C#. Ok, now onward..

    Please look at this page for how to insert code nicely into the forums: https://forum.unity3d.com/threads/using-code-tags-properly.143875/

    Your code doesn't require you to click the cube to open the menu - is that what you want? You can just click anywhere?
    Could you post your code that's responsible for showing/hiding the child cube?
    I think this could be redone to much easier.

    For instance, if you use the EventSystem, you can detect a click on the cube, if that's what you want.. if you want the click to be anywhere, that's okay, too. Whichever way you choose, you can have a public variable for the game object (child cube), which you can then setActive (true or false) when your click is detected, as opposed to 'hideme'.

    As for the specific error, it's saying you don't have a 'closeme' component found on the object or child object.
    Either this script is on the wrong object, or the 'closeme' is misspelled maybe.

    Since I don't know unityscript well, I don't know if this next part is correct, but in C#, you'd type: GetComponent<closeme>() (provided "closeme" is the correct name). Notice the different brackets.
     
  3. tim76

    tim76

    Joined:
    Nov 11, 2014
    Posts:
    20
    Not new to coding, just new to coding in unity, and I don't know all unity api's calls and how they work.
    Also I know a little C# I just prefer javascripting. Javascripting seems like less typing.

    This is not actually for a game but and an app that would calculate voltage drop within a lighting system. I can't find software for this nor can I find another ide that allow for as much runtime manipulation as unity, so this why I am using it. Anyway I am trying to make a visual scripting type app, very much like the one in the Unity ide.

    I want to make it where a user can place 'cubes' on the screen and connect cubes to one another collecting info from the connected objects to run calculations. I have 3 different types of cubes and each type would have their own set of variables. So I was thinking to put Canvas on each cube types to be the menu to set the variables for that cube. I would have one cube type for a circuit breaker, one for a junction box, and another for a light fixture. Would a canvas not be the best way to do this?

    Btw I found the following code for the above question...

    Code:

    if(Input.GetMouseButtonDown(1)) {

    var meeple = this.gameObject.transform.GetChild(0);
    var ChildComp = meeple.GetComponent(closeme);
    ChildComp.hideme = true;


    }
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Doesn't sound like anything wrong with using a canvas to show variables for each cube if that's what you want.
    As for the new code you found, it looks just like the last bit of code with an intermediate step. Unless your object had a "closeme" script on the parent, as well, they would both work the same.
     
  5. tim76

    tim76

    Joined:
    Nov 11, 2014
    Posts:
    20
    I this first one didn't work, the 2nd on did....I don't know why.