#Wrong sprite assignment
1 messages · Page 1 of 1 (latest)
When assigning the sprite, print CamilaLvl2 to the console
ren.sprite = CamilaLvl2;
so after this line of the script, I should print a sprite to the console?
Yes, it doesn't matter whether after or before
Null
What does it mean?
that its not assigned
Right, so assign it
its assinged in the canvas but not assigned in the lvl1 objects that are spawning during the script
CamilaLvl2 its spawned during the collision
!code
📃 Large Code Blocks
Use 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 format as C#, add cs to the first line:
```cs
// Your code here
```
Add a comment with a line number if there is an error message.
public void Create(int level)
{
GameObject myGameObject2 = new GameObject("Lvl" + level.ToString());
ren = myGameObject2.AddComponent<SpriteRenderer>();
ren.sprite = CamilaLvl2;
Debug.Log(CamilaLvl2);
bc2d = myGameObject2.AddComponent<BoxCollider2D>();
rb2d = myGameObject2.AddComponent<Rigidbody2D>();
rb2d.gravityScale = 0;
bc2d.isTrigger = true;
}```
the renderer boxcollision and rigidbody settings are good
but the image isnt assigning
actually make sense
but when?
Ill debug in the udpate
Ohhh
When I spawned the first one
Then it goes null
It shouldn't
Ill send you a video with the console
Remove the global variable for ren and assign the added sprite to a newly created one
Please, send the code
Alright
📃 Large Code Blocks
Use 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 format as C#, add cs to the first line:
```cs
// Your code here
```
Add a comment with a line number if there is an error message.
Via site
That's why I asked you to use a site
or by parts?
I don't want to download it
I suppose, I have asked you to use a suitable site 2 times by now
The ones sent here
alr
Use a suitable site to paste your code
Save it and send the link
Yes.
Lmk if you find anything that isnt alr
Where's the other code?
Found it, my bad.
no worries
From what I see, the sprite should not be null
Right
when spawning the first
its going to null
but in the same time has somehting (idk how)
Now change it:
void Update()
{
Debug.Log($"camilaLvl2: {CamilaLvl2}; this: {name};", this);
}
this.name is the name of the current GameObject
You can see that camilaLvl2 is only null Lvl1, not on Canvas
This should now be enough for you to figure it out
in a way yh
in a way no
why do I need to assign in the same time in the camilalvl1?
when I take the sprite from the canvas?
and how can I do it?
This happens, because you create a GameObject called Lvl1,
GameObject myGameObject = new GameObject("Lvl1");
which you then add your BuyScript to
myGameObject.AddComponent<BuyScript>();
and I have to assign all of the thing to the gameobject if I create a gameobject with that script?
Your variables are assigned by you in the Inspector, so all of them are null when simply adding the script
Indeed
when using it?
Not necessarily, you can Instantiate a prefab
A guy yesterday told me about a prefab
This guy was correct.
but didnt mention how to properly use it, I tried looking for it on the internet and 0 answer
I couldnt find a way to actually use it
Prefab is basics, so I'm sure there is a lot of information on how to use them on the web
But a thing its different here
I create that thing during the script
I had an idea yesterday maybe u can tell me if its correct
run the game
spawn a lvl1 gameobject
take it inside of the folder
add the sprites to it
and then add it to the game?
instead of the gameobj?
-
- Drag the suitable prefab into a suitable folder in
Assets
- Drag the suitable prefab into a suitable folder in
-
- Assign the prefab to the required script
-
- Use the
Instantiatemethod with it as the 1st parameter
- Use the
The sprites should be assigned directly to the prefab, but it's important to note that those sprites cannot be a part of the Scene
Not sure what you mean
like if I want to add a button from the scene
I cant
cause its in the scene
all the UI basically that its in the scene
so basically
right now I created a prefab
with the camilalvl1
I added those spritess from the folder in it
Until now, all its good right?
Are you sure you even need to use a prefab?
What I mean is, are you sure you even need to instantiate a prefab with the script?
yeah
cause in that script
is a drag feature
and I cant create a new script with the drag feature
cause i need some things from this script
so basicallyt yeah I need it
Are you sure the script should contain all of your sprites in it?
not all of them
I only need the camilalvl2
the camilalvl1 can be taken from the canvas
the camilalvl1 can be the only one without a prefab
cause when I add the script to the camilalvl1, the camilalvl2 sprite is going to null
its alr until now?
Why do you need camilaLvl2?
the gameobject or the sprite?
the gameobject cause its part of the game
the sprite cause its the image
I need them both
Why?
why do I need the gO?
Yes, why do you need the reference to it?
Im not quite sure what do u mean by reference to it
like I need the gO cause I want it in my game
cause its part of it
and the sprite, cause I want it to be rendered
What are you doing with your GameObject?
the lvl1 or the lvl2?
Both
playing with them?
like its part of the game plan
im not quite sure what u want to get from the question
like i dont know what answer do you expect from me to give?
What are you going to do with this GameObjects?
What I'm talking about is, you don't need all your prefabs to contain a reference to the GameObjects, if they're the same for each of your prefabs. Instead, you should create a main script, which may usually be a Singleton with the DontDestroyOnLoad functionality, and reference all the required objects with the instance.
as you could see in the mp4 I send in to you
its basically a merge
I spawned 2
Then I merged them
And i want sometrhing in return
And with that thing, Ill merge it again
and again
-
- You should have a class, which manages all this merge. You handle dragging with the
OnMouseDragmethod, so it should call the methods from this class
- You should have a class, which manages all this merge. You handle dragging with the
-
- These camels should have their
ScriptableObjectclass, so that you can then create new items freely
- These camels should have their
But, actually, you should really start from the tutorials now. !learn
:teacher: Unity Learn ↗
Over 750 hours of free live and on-demand learning content for all levels of experience!
Ok so
Looking on a tutorial of how prefabs work
prefabs are like gameobject with the settings modified alr of another gameobject
You may start from the site I've sent you
Pivot is where the position starts from.
The default pivot is (.5, .5), which means the position is in the middle of the object. Having the pivot at (1, 1), sets the position to the top-right corner
Ok so basically my friend
I got another problem if u can help me with it
@dull thistle
I got a prefab that is anchored that is a child of another gameobject that is anchored and has a pivot in order to let that prefab to move. Even with all of those, when switching the resolution, he s still moving. Why s that?
What does "not moving" supposed to mean? Why does it have to move?
what do you mean by that?
I didnt say not moving
I did say its moving
and I dont want
So the Clone is the prefab and the 1 its the carpet
this is the carpet
Alright. The children of the prefab are always going to be moved with the parent
Exactly
Right, so what's the problem?
The child moves if you're moving it
Im not moving it
So why does it move?
Let me clarify the whole situation
This is the script that spawns the gameobject
This is exactly what happens in the Position
So im moving the pivot of the parent in order to move the child
Both of them are anchored
Why does THE CHILD moves if Im not moving the parent?
The parent stays at his place anchored
Why does the child moves
Haven't you said that you're moving it?
All Im doing is that im switching the pivot
in order to put the cammel in place
not the actual carpet
The child's position is adjusted according to their parent's pivot, so changing the pivot also affects the children's positions
alright
if im putting a pivot to the parent in order to change my child position, wont be exactly in the same place no matter what the resolution?
It will be in exactly the same place in world space, but it doesn't mean it will be visible on the screen in both cases
damnn
let me see if its working
Using the world space
they are staying all in place and just the sides goes in all the place
its not good
Maybe If I use the Overlay?
Instead of the Camera?
What are you even trying to accomplish?
A game with all the things in UI placed where they supposed to be
like the carpets are staying good cause I anchored them to the canvas
but the prefab im spawning that is anchored on each carpet
each carpet has a pivot
If im switching the resolution, the prefabs are going in other directions
If you have multiple UI Objects to align, use Grid Layout Group, Horizontal Layout Group or Vertical Layout Group components according to the alignment objective
No, it doesn't.
can you tell me which part you didnt understand please?
What's the problem?
@dull thistle
The object is not supposed to stay on the same place on drag just by changing its pivot and anchors
what do you mean on drag?
I mean changing its position by dragging it
What do you mean? How can you change the Transform's position by dragging it?
no
I mean that
I made that camel (prefab) child of a gameobject
how to fix it in a place no matter what the resolution if you can help me
I tried to put a pivot (didnt work)
I tried to anchor it (didnt work)
what should I do?
@dull thistle
You should change its anchor presets
You should show it
Not sure why you have mentioned it only one day after. Seems like you are doing it the wrong way
the white line is the widtha nd ehight of the carpet
if i put the anchor to the left side it ll put the camel on the carpet
idk why
if i put the weight and height exactly on the carpet, the cammel with the anchor of top left ll go on the bottom right
and idk why
Why are those even UI?
Well, they can be. Right.
From what I've seen on the video above, you can just put Screen Space on your Canvas's Render mode and Scale with Screen Size on UI Scale Mode
SpriteRenderers have neither pivots nor anchors
And rotation
ye
ok so
I got the carpet
that is a SpriteRenderer
And I also have the Camel
That is a prefab
with Sprite Renderer
The carpet is fixed into the screen cause I anchored it
but I could anchor it to the background
how could I ?
How could it have it? It has to be UI
If you deal with resolutions, you usually use UI for this
You either use SpriteRenderer as non-UI, or Image as UI
Its staying in the same place
There is nothing in-between
indeed
Then correct it
but I got a sprite renderer that its anchored and it doenst matter the resolution
and u said that those cand have anchors
Remove the SpriteRenderer and add an Image
to the carpets?
Everywhere this appears
ok so now every carpet is a UI
like it has the Image component
with the source image assigned
and now as soon as I put the anchor to the left side, the cammel should be put where?
Yes, as long as you also hold Alt to alter its position
nope
I cant do that over the script
I cant press alt during the script
if I hold it, yeah
but on the inspector
I can to modify the anchormin and anchormax
but its not moving his position as well
Are you trying to set camel's position using anchors?
anchor and pivots
🇼 🇠🇾
if I switch the carpet pivot, its moving the carpet as well
what do you mean why?
with what am i suppose to move it?
With.. position?
📃 Large Code Blocks
Use 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 format as C#, add cs to the first line:
```cs
// Your code here
```
Add a comment with a line number if there is an error message.
It would be good if you could set its parent and assign the position to 0
No, read my previous response
it would be good if you good?
typo?
cause I dont understand it properly
"could"
oh alr
That should still be enough to understand
!code
📃 Large Code Blocks
Use 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 format as C#, add cs to the first line:
```cs
// Your code here
```
Add a comment with a line number if there is an error message.
var newObj = (GameObject)Instantiate(Resources.Load("Lvl1"), transform.position, Quaternion.identity);
newObj.transform.SetParent(carpet1.transform);
FirstOccupied=true;
RectTransform uitransform = newObj.GetComponent<RectTransform>();
uitransform.anchorMin = new Vector2(0f, 1f);
uitransform.anchorMax = new Vector2(0f, 1f);```
this is my code for spawning and assign it as a child of the carpet
wdym by assigning the position to 0
the position of who?
If you use UI, you must
-
- Have a
Grid Layout Groupof your carpets
- Have a
-
- Have a
Listof those carpets in yourManagerscript
When changing the camel'sposition
- Have a
-
- Parent it to the desired carpet
-
- If, for some reason, its
positionisn't zero, correct it
- If, for some reason, its
What do you think?
of the cammel
cause if its 0, it goes to the left side of the carpet
For some reason its like 200 and -600
its spawning like that
Show the code
Right now Im var newObj = (GameObject)Instantiate(Resources.Load("Lvl1"), transform.position, Quaternion.identity); newObj.transform.SetParent(carpet1.transform); FirstOccupied=true; RectTransform uitransform = newObj.GetComponent<RectTransform>(); uitransform.anchorMin = new Vector2(0f, 1f); uitransform.anchorMax = new Vector2(0f, 1f);
Do you mind also shifting it?
Why are you setting the anchors?
This is not needed
what do you mean also shifting it?
var newObj = (GameObject)Instantiate(Resources.Load("Lvl1"), transform.position, Quaternion.identity);
newObj.transform.SetParent(carpet1.transform);
FirstOccupied=true;
RectTransform uitransform = newObj.GetComponent<RectTransform>();
uitransform.anchorMin = new Vector2(0f, 1f);
uitransform.anchorMax = new Vector2(0f, 1f);
var newObj = (GameObject)Instantiate(Resources.Load("Lvl1"), transform.position, Quaternion.identity);
FirstOccupied=true;
RectTransform uitransform = newObj.GetComponent<RectTransform>();
uitransform.anchorMin = new Vector2(0f, 1f);
uitransform.anchorMax = new Vector2(0f, 1f);
newObj.transform.SetParent(carpet1.transform);
so like this
var newObj = (GameObject)Instantiate(Resources.Load("Lvl1"), transform.position, carpet1);
FirstOccupied=true;
RectTransform uitransform = newObj.GetComponent<RectTransform>();
uitransform.anchorMin = new Vector2(0f, 1f);
uitransform.anchorMax = new Vector2(0f, 1f);
newObj.transform.SetParent(carpet1.transform);
like this?
Yes
Alright, no
I don't remember the order of the parameters
You could at least use docs or Ctrl + Shift + Space to check the parameters yourself
right now the problem is that why the cammel is spawning at like -500 -700?
instead of 0 0
Well, because you spawn it at transform.position?
You should have done that already
done
ok so
I got the grid layout group
exactly as it was before
this is how it spawns
Right, so what position does it spawn at?
741.3 -538.2
FirstOccupied=true;
newObj.transform.SetParent(carpet1.transform);
RectTransform uitransform = newObj.GetComponent<RectTransform>();
uitransform.anchorMin = new Vector2(0f, 1f);
uitransform.anchorMax = new Vector2(0f, 1f);
newObj.transform.position = new Vector3(0, 0, 0);```
@dull thistle
Are you going to answer this one?
0 0 0
trough the code
but its still not 0 0 0
First of all, why don't you spawn there directly in the Instantiate method?
Secondly, transform.position takes World space position as its value
This means, the position is not relative to the parent
If you want to make it relative, use a more suitable method
what other method exists?
transform.position = transform.TransformDirection(position);
transform.position = -parent.position;
No, I don't
I mean that parent should be Transform
This is a worse method
I also forgot about the best one
transform.localPosition = Vector3.zero;
Use Vector3.zero or default to simplify
using this one
Why is the transform's position assigned?
Yes, show the actual code you're using right now
this is the best one
Yes, when not trying to get a specific variable
This does the same as the first variant directly in the setter
{
var newObj = (GameObject)Instantiate(Resources.Load("Lvl1"), transform.position, Quaternion.identity);
FirstOccupied=true;
newObj.transform.SetParent(carpet1.transform);
RectTransform uitransform = newObj.GetComponent<RectTransform>();
uitransform.anchorMin = new Vector2(0f, 1f);
uitransform.anchorMax = new Vector2(0f, 1f);
newObj.transform.localPosition = Vector3.zero;
}```
Yes, this should set it correctly
The anchor.
I wasn't paying attention to them now, but I told you you would remove the anchor asignment
FirstOccupied=true;
newObj.transform.SetParent(carpet1.transform);
newObj.transform.localPosition = Vector3.zero;```
so like this
so I dont need any anchor?
Transform newObj = Instantiate((Transform)Resources.Load("Lvl1"));
FirstOccupied = true;
newObj.SetParent(carpet1.transform);
newObj.localPosition = Vector3.zero;
Have you check that already?
You do need, if you want
You don't, if you're telling me it's not spawning at Vector3.zero
not working
working
spawning at like -2 and 6
Are you saying only this version works properly?
Oh, mine?
yes
Then the Transform convention isn't possible
That's strange, but change it to GameObject
only working with var
not even with go
nope
not even with var
as in transform
and I got this
when using this
That's not what causing the issue
ik
Does this not work properly?
this warning
Does it appear when running the code?
yes
Has is appeared before?
Does it disappear when removing the code you've previously sent?
nope
tf
I had the same exact script without the warning
so nope
The issue does not disappear when removing this part of the code?
Then reload Unity
oke
Seems like a Unity's bug, which can even be ignored
Please, send the full warning
as text
Unable to use UDP port 56886 for VS/Unity messaging. You should check if another process is already bound to this port or if your firewall settings are compatible.
UnityEngine.Debug:LogWarning (object)
Microsoft.Unity.VisualStudio.Editor.VisualStudioIntegration/<>c:<.cctor>b__6_0 () (at ./Library/PackageCache/com.unity.ide.visualstudio@2.0.22/Editor/VisualStudioIntegration.cs:55)
Microsoft.Unity.VisualStudio.Editor.VisualStudioIntegration/<>c__DisplayClass8_0:<RunOnceOnUpdate>b__0 () (at ./Library/PackageCache/com.unity.ide.visualstudio@2.0.22/Editor/VisualStudioIntegration.cs:100)
UnityEditor.EditorApplication:Internal_CallUpdateFunctions ()
Try to google it and see if any solutions work
Because I have never seen it before
Also check your firewall first
about?
I had both VMware and VirtualBox, and uninstalling both of them solved both the unable to use UDP port for vs/unity messaging and the unable to join player connection multicast group issues.
i dont think I have any of them
You should check the settings on your computer for the firewall
yee
@dull thistle
i disabled my firewall and its not giving me anymore
but i cant stay like this
its alr
I made it
Nice
What's with it?
📃 Large Code Blocks
Use 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 format as C#, add cs to the first line:
```cs
// Your code here
```
Add a comment with a line number if there is an error message.
FirstOccupied=true;
newObj.transform.SetParent(carpet1.transform);
newObj.transform.localPosition = Vector3.zero;```
Yeah, it's absolutely fine. It's positioned at the center of this thing
Alright
You have to set your camel's y pivot to 0
The pivot should be changed directly on the prefab
That's right
No, x should stay at 0.5
the cammel aint moving
Why is the position not at 0?
📃 Large Code Blocks
Use 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 format as C#, add cs to the first line:
```cs
// Your code here
```
Add a comment with a line number if there is an error message.
FirstOccupied=true;
newObj.transform.SetParent(carpet1.transform);
newObj.transform.localPosition = Vector3.zero;```
this the code
I didnt change anything
and on the prefab is 0 0
Change the position manually and show the screenshot
Yes
like the y
With your hands
to move it upwards?
No, set it to 0
And make a screenshot, also covering the Inspector
position of the pivots u mean right?>
value of x pivot
cause u said y pivot to be 0
Yes, change the position of the camel manually
And send me a screenshot of how fine it's looking on the screen
the position not the pivot
Yes
Show the hierarchy
Now show the object named 1
And in the scene
Press T to select Rect Tool
press T where?
On your keyboard
i mean in which screen
Try to follow your muscle memory with your eyes
The scene, still
cause pressing T on the unity aint doing anything
That's where the rect tool is
Yes
ok
Now show your camel at (0, 0) with pivot (0.5, 0)
Why is it still using SpriteRenderer?
Yes
No.
Point out the message, where I have mentioned that
its like 10 days ago
I have said "use SpriteRenderer with non-UI objet, and Image with UI"
Why are you even continuing this if you don't understand what I'm saying at all?
UI is everything inside of the Canvas
User Interface
Yes.
That's what I've told you at least 15 times
My mood
What?
its not
my bad
i had it transparent
WHAT THE FUCL
why by using image it lifted it up
it doesnt make any sense
nothing it makes sense anymore
Yes, it doesn't make sense, even though I told you this countless times
WTTFFF
its working but why
ok so all we did
was to put Image to the prefab
put Image to the carpet
put that thing to 0 0 0
You are lucky I'm listening to some really kind music right now
Im so fucking lucky
If I was you, I would kick my ass
I would jump off a cliff tbh
Yes, that's a pretty accurate representation of my plans tonight
🤣
Yeah unfortunately
these type of dumb people still exist
extinct actually
how old are you?
Alright, I go have a sleep. Good luck with your game
ill come with you
the scale is chaning itself