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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How do I reference variables from other voids in a script?

Discussion in 'Scripting' started by Xander_8002, Jul 18, 2022.

  1. Xander_8002

    Xander_8002

    Joined:
    Jul 1, 2022
    Posts:
    13
    I am making a game and I need to have a
    void OnTriggerEnter(Collider1 Collider2)

    I tried things such as Collider1.IsTouching(Collider2) But then realised that that was only for 2d games which doesn't work as mine is 3d.

    In this case, Collider1 and Collider2 are both variables that I define part way through void Update().
    Since these voids are both in the same script is there a way I can get the Colliders from the void Update to the void OnTriggerEnter?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Kurt-Dekker likes this.
  3. Xander_8002

    Xander_8002

    Joined:
    Jul 1, 2022
    Posts:
    13
    i am fairly new to coding yes. but giving me an 'answer' by giving tutorials that take multiple hours to complete is completley useless to me. I have got most of the basic ideas of C# so answering the question with the actual answer is more beneficial than sending me to hours of tutorials.
    I learn by looking at what I need to do and replicating it... not sitting for hours looking at things I have already done just to find one thing that I need.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    You don't know what methods are
    You don't understand variable scoping

    I think you are vastly overestimating your level of C# knowledge and would greatly benefit from those tutorials.

    Also nothing is stopping you from jumping around the tutorials. They are self-guided.
     
    Kurt-Dekker likes this.
  5. Xander_8002

    Xander_8002

    Joined:
    Jul 1, 2022
    Posts:
    13
    I am not saying I am good, I am saying that right now I am looking for the answer to my question not tutorials. I do not understand scoping but that is why I am here, not to find out other information. If you know the answer, why are you being difficult.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,951
    Sounds like you're doing it wrong. Don't worry, it's pretty common to do tutorials incorrectly.

    Pay particular attention to Step #2 below:

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!

    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.


    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
     
  7. Xander_8002

    Xander_8002

    Joined:
    Jul 1, 2022
    Posts:
    13
    This is useful but one problem... I am not following a tutorial for this part of the game as I couldn't find a single tutorial that does what I want
    To be fair, i have never seen a game do what i am doing because it is quite frankly a strange thing to do
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    I prefer to teach people to fish rather than giving them fish.
     
    Kurt-Dekker likes this.
  9. Xander_8002

    Xander_8002

    Joined:
    Jul 1, 2022
    Posts:
    13
    However, i learn by being given the fish, looking at it, understanding it and then remaking it.
    The problem is that I learn differently to how you teach and if you as the teacher understand but try to teach me differently, I can't learn like that. If you are able to teach me the way I learn, then that is easier than me trying to learn the way you teach.
     
  10. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Xander_8002 likes this.