#Gameobject.add in a while loop not working the same as a debug statement

1 messages · Page 1 of 1 (latest)

lapis hinge
#

objectsInLine.Add(trueRaycastHit2D[nameTic].transform.gameObject);
Debug.Log(trueRaycastHit2D[nameTic].transform.tag);

#

the second line works as intended but the first one inputs the first object the raycast hits in every place after 2

#

this is in a while statement and name tic is inceased every fame up to 4

#

also note that they are right next to each other

misty tinsel
#

show your !code

solar dewBOT
#
Posting code

📃 Large Code Blocks
Large code blocks should be posted as links to services like:
https://gdl.space/, https://paste.ofcode.org/, https://hatebin.com/
https://paste.myst.rs/, https://hastebin.com/

📃 Inline Code
Surround code with three backquotes. Not quotation marks.
To get C# formatting the first line should only contain cs or csharp.
Add a comment with a line number if there is an error message.
```cs
// Your code here
```
Do not share screenshots of code unless requested.

misty tinsel
#

not just one line

lapis hinge
#

ok

#

should i put it all in there?

misty tinsel
#

The entire script.

misty tinsel
lapis hinge
#

it is on line line 80 and 81

misty tinsel
#

Is there a Rigidbody2D on a parent of these objects?

lapis hinge
#

no

misty tinsel
#

I believe RaycastHit2D.transform gives you the transform of the parent rigidbody2D, if one exists

#

instead of logging transform.tag, just log transform

#

that will be more useful

lapis hinge
#

that doesnt fix it though

misty tinsel
#

of course not, but it'll change what you're logging

#

do you see a different Transform being logged every time?

#

or are you seeing the same Transform many times?

lapis hinge
#

I have all of the objects named the same thing so i need to edit the code to allow me to differentiate the names

#

give me a sec

misty tinsel
#

alternatively

#

Debug.Log("Hit: ", trueRaycastHit2D[nameTic].transform);

#

Debug.Log can take two arguments

#

The second argument is the "context". It can be any unity object

#

Clicking on the log entry will take you to the object in the hierarchy.

lapis hinge
#

ok thanks

misty tinsel
#

I had this exact issue before I found out about the second argument

#

was trying to find one object out of thousands I instantiated

lapis hinge
#

it is throwing an error

#

cannot convert from void to object

misty tinsel
#

show me your code.

lapis hinge
#

i have no idea where it is getting void from

misty tinsel
#

I don't know what you wrote

#

you can just show the line.

lapis hinge
#

nvm i was bieng an idiot

#

i wrote debug.log twice

misty tinsel
#

ah, so you tried to pass the result of Debug.Log into the second argument

#

and it returns void

lapis hinge
#

yes but now it is logging

#

It is showing the same problem as the other line now

misty tinsel
#

Do you have many colliders on the same transform?

lapis hinge
#

nvm again the debug log is working as intended

#

i looked at the wrong part of the log sorry

lapis hinge
#

are you still there?

lapis hinge
#

i fixed it