Search Unity

Question How to Access a specific gameObject

Discussion in 'Scripting' started by Ralz132231, Dec 14, 2022.

  1. Ralz132231

    Ralz132231

    Joined:
    Jan 6, 2022
    Posts:
    4
    Hello I am trying to make a script where two with the same script collide they should only send one signal for an example they together should only set one bool in there own script to true and it looks like this

    if (other.GetComponent<RoomSpawner>().spawned == false && spawned == false){ spawned = true; }

    But here comes the problem I dont know how to do with three because if I do

    if (other.GetComponent<RoomSpawner>().spawned == false && other.GetComponent<RoomSpawner>().spawned == false && spawned == false){}

    they could choose the same other twice and I don't want that pls help
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,692
    Assuming
    other
    is an argument incoming from a Trigger or Collision callback,
    other
    will only be one GameObject, the one involved in the collision callback.

    GetComponent<T>() only gets ONE component from
    other
    .

    If the component type does not exist on other, it will return null, which you may wish to detect before blindly attempting to access fields on it.

    Also, you should not write massive long lines of code like the above statement.

    If you have more than one or two dots (.) in a single statement, you're just being mean to yourself.

    How to break down hairy lines of code:

    http://plbm.com/?p=248

    Break it up, practice social distancing in your code, one thing per line please.

    "Programming is hard enough without making it harder for ourselves." - angrypenguin on Unity3D forums

    "Combining a bunch of stuff into one line always feels satisfying, but it's always a PITA to debug." - StarManta on the Unity3D forums

    Whatever combination of things you're trying to do, it WILL require more than just code: it must be set up properly in the editor as well, so rather than me guessing at your setup, it's best for you to go work through a complete Youtube tutorial for whatever kind of spawner you want here.

    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!

    Finally, when you have errors, don't post here... just go fix your errors! Here's how:

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.
     
  3. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    720
    You'll need to provide a little more information. Collisions only ever happen in pairs: A and B can hit each other, but you can't have a collision between A, B, and C all at once.

    Do you want to detect when three objects are overlapping with each other and set a variable on just one of them?

    If so, I think you'd need to have each object keep a list of other objects they've collided with (adding things with the "on enter" function and removing things with the "on exit" function). You could then do something when that list contains enough things.

    This is a fair bit more complex than the two-object case. You have to keep track of things for more than a single instant!

    If you want to detect a collision between two objects, where one object could many instances of RoomSpawner on it, then you would want to get all of the RoomSpawner components from the other object. There's a function with a very similar name to GetComponent that could help you there :)

    You should definitely explain what you're trying to do here, though -- it sounds a bit weird to me. There might be a better way to handle this!
     
    Last edited: Dec 14, 2022
  4. Ralz132231

    Ralz132231

    Joined:
    Jan 6, 2022
    Posts:
    4
    Okay I think I was super unclear and should explain thw whole backstory I just didnt want to confuse things too much.
    okay so I am trying to make a dungeon generator I have made so the rooms can spawn and when two rooms collide with eachother they spawn a room with the doors that was needed I dont need help with where the rooms are suppose to be or how to spawn them the only issue I have is that I am verry new to unity and are trying mostly to learn and not understand how I should do so when 3 of the same object collide with eachothers they will add to all three of them to only one script but all the three gameObjects have the same script so I need how to send over the three things to only one script and that is the problem. I think that I use the wrong solution but this is the only one I am thinking of.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,692
    Just so you know, this:

    is not even REMOTELY a trivial thing.

    As with any engineering problem, NONE of it will be done all-at-once

    As with any engineering problem, YOU are never the first one.

    If you need guidance, start with any of the over one million dungeon generator videos out there already.

    Nobody is going to hammer one of those back into this little text box for you.

    Screen Shot 2022-12-14 at 12.44.08 PM.png

    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!

    Finally, when you have errors, don't post here... just go fix your errors! Here's how:

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.
     
  6. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    720
    Ah, funny thing: I was working on a very similar problem a few weeks ago. I was figuring out how to build a graph of procedurally-generated rooms, so that I could decide which rooms were accessible from which other rooms. I wound up realizing that I could just use the Navmesh system for this, though...

    I think you just need to reframe your problem a bit. You need to do something when two rooms overlap, and you also need to make sure that, for each pair of rooms, you only do that thing once, right?

    A very simple solution would be to give each room a number, starting from 0. When two rooms overlap, the room with the smaller number "wins", and gets to do something (e.g. spawn doors).

    To assign those numbers, you'll want to use a static variable -- one that's shared among all instances of a class. So, you might have something like...

    Code (CSharp):
    1. class Room {
    2.   private static int nextIndex = 0;
    3.   public int index;
    4.  
    5.   void Awake() {
    6.     index = nextIndex;
    7.     nextIndex += 1;
    8.   }
    9. }
    Every room has a unique number (assigned during Awake), so for every pair of rooms, there is one -- and only one -- winner. The exact numbers don't really matter, as long as each room gets a unique one.

    But if I've misunderstood, and you do want to detect that, say, three rooms are all touching each other at the same time, then this definitely gets a bit more difficult :p
     
  7. Ralz132231

    Ralz132231

    Joined:
    Jan 6, 2022
    Posts:
    4
    I think I am on to something I just need to steal a value I havent figured that out yet. To what you are saying i could just check the instanceID. Okay I got it to work now only problem is that it invokes 3 times i'll dont really now how to fix that but heres the code

    void OnTriggerEnter2D(Collider2D other){
    if (other.CompareTag("SpawnPoint")){
    if (other.GetComponent<RoomSpawner>().spawned == false)
    {
    OpeningDirection.Add(other.GetComponent<RoomSpawner>().openingDirection);
    }
    if (spawned == false)
    {
    spawned = true;
    OpeningDirection.Add(openingDirection);
    Invoke("ConnectingRooms", 1f);
    }
    }
    if (gameObject.tag == "SpawnPoint" && other.CompareTag("Room"))
    {
    Destroy(gameObject);
    }
    }
     
  8. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    699
    When dealing with multiple stuff it can be easier to replace the bool with an int.

    so here you would do spawnedCount++ and if (spawnedCount > 1) Destroy(...
     
  9. Ralz132231

    Ralz132231

    Joined:
    Jan 6, 2022
    Posts:
    4
    Okay got everything figured out thanks to everyone who tried to help me
     
    Kurt-Dekker likes this.