So I've noticed that in order to use properties of SpriteRenderer, you need to get the component first but the same isn't done for Transform.position. Isn't Transform another component like SpriteRenderer?
Yes, Transform is just another component of the system. The same is about RectTransform, which is used in the unity UI system (canvas).
Several years ago as far as I know, transform property was just syntactic sugar over GetComponent<Transform>() code, as any GameObject has this property and it is accessed quite often. Also, because of the underlying GetComponent call, which is not completely free there was a recommendation for a long time to cache this property if you need to access it often (assign it to a class member and use this member instead of this.transform).
Right now, as far as I know, caching has no sense in most cases, as all components are stored in the array internally, and Transform always has an index 0, so accessing transform is just an access to the first element of the array. Anyway, if someone has really tons of calls to it, it can be useful to profile this, but I don't think it will be an issue in most cases.
But some dinosaurs like me still cache it out of habit :D
Related
I needed to find a better way to find my Game Objects without using GameObject.Find because I have this line more than 30 time is one script, so I created this line on my script:
public List<GameObject> gameObjectList = new List<GameObject>();
Then, I go in the inspector and add manually the gameobjects in the list.
and everything works fine.
But I see nobody talking about that way on internet, so is there something wrong with that way?
Assigning Object references using the Inspector is a great practice! It can make your components more flexible, because you can rewire the same code to work with different Objects.
Note that you can also expose private fields using the SerializeField attribute. Making fields private when you can is good practice, because limiting access to them can help reduce bugs.
Another things to note is that you can also edit Component type fields using the Inspector, so you don't need to do any GetComponent calls in your code. You rarely need to work with GameObjects directly; basically only when you want to destroy a GameObject or modify it's active state.
[SerializeField]
private Button[] buttons = new Button[0];
There is a simulator. In this simulator we have to pass a corridor. In the corridor there is a door and a puzzle, the solution of which opens this door. As soon as we solve the puzzle, the value of the boolean attribute of the class (something like isOpen) changes to true
This corridor needs to be traversed several times. The corridor itself doesn't change, but the puzzle is random each time.
So, I decided to create a macro application that reaches the puzzle and waits until I solve it
And since the simulation has the boolean variable I need, I was wondering: can I get it, in order to then create a delay in the macro until it is true?
The main problem here is that the two programs are not connected in any way.
I also want to note that I have an understanding that all variables lose their names after compilation, and that variable values subsequently occupy a random place in memory
Also, I have experience with programs like CheatEngine, which is to find the address of a value by its value
But I may just not know all the details, thinking that it's impossible, even if in reality there are ways to do it.
For this reason, I would appreciate it if you could explain to me how this can be done, or, if it is not possible, explain why.
Also, I wouldn't mind a response like "Read this "
I understand that you want to inspect one or more properties of an instance of an object at runtime and this can be achieved by using the so-called Reflection.
The latter provides functionalities that allow you to examine objects at runtime, get their Type, read their properties and invoke their methods. It should be used carefully.
Using reflection you can do
// retrieves the value of the property "NameOfProperty" for the instance of object myobj
bool myFlag = myobj.GetType().GetProperty("NameOfProperty").GetValue(myobj, null);
If I have a GameObject with multiple Component classes attached to it of the same type (let's call this type HingeJoint for example purposes), which HingeJoint component will I get if I called the function GetComponent<HingeJoint>() on my GameObject?
According to my research, this answer claims Unity will simply return the first match from the array of Components--however, the answer is only an educated guess based on the answerer's own game engine design that strives to mimic Unity and not an authoritative source for what Unity actually does.
Every object and it's subobjects in Unity are hierarchically placed in a collection. You can call it a "WYSIWYG" because as you see every object in the scene hierarchy is loaded in the same order they are displayed on that list. The same thing applies to the components and it's the main reason why Transform component is placed on top of every other component.
GetComponent will return the first item from the collection of Components seen from the top of the list. To be exactly sure which component will be returned you can order them as you wish in the inspector view.
From the documentation page:
The order you give to components in the Inspector window is the order you need to use when querying components in your user scripts. If you query the components programmatically, you’ll get the order you see in the Inspector.
EDITED after m.rogalski correction
The manual indicates that components are checked in the order you put them in the inspector, so GetComponent() will give you the first one of type T
How ever I would recommend not to use inspector ordering, but instead use
GetComponents<T>()
To retrieve them all and dynamically choose the one you need (See the doc if needed)
This is because it would make it really difficult for you to maintain your project if the order of your components mattered. Any other person working on the project would need to know the convention in which these components must be placed. Even if you work alone, you would need to be very careful when adding or removing components, and in case of a bug, your IDE won't be able to give you a clear error
I am trying to implement this tween class for my custom game framework, I don't exactly know how to use it.
(The framework is pretty similar to XNA).
This tinyTween class seems very complete, but I cannot quite understand it.
https://gist.github.com/liaoguipeng13/717f83f4971230e70d7e
http://theinstructionlimit.com/flash-style-tweeneasing-functions-in-c
Should I instantiate the tweening class? or can I use it without instantiating it?.
Also for moving sprites I can set in the update my functions
SetVelocityX, SetX, SetAccelX etc...
I am interested in making a sprite move from point (100,150) to (400,600) with a nice moving effect...
Should I instantiate the tweening class?
According to the Tween class, yes, you need to instantiate the class. Though as you can tell by all the classes that inherits from it, you can most likely use one of those for whatever you're planning on using it for, instead of making your own implementations.
I am interested in making a sprite move from point (100,150) to (400,600) with a nice moving effect...
Based on the Tween class, it seems very simple. All you gotta do is call Start with the start and end position, a duration, and a formula for movement. The Tween class should handle the rest, so long as you remember to call Update every frame.
I'm trying to create a system in which numbers are changed according to the time elapsed since last update, their target/destination value, etc.
In an earlier project, I created an abstract class for animation data which contained Tick(), IsDone(), etc, which were then implemented for each facet of the game object I wanted to animate, such as position and opacity. The animation data were held by the game object, with the Tick() function being called by the game object's Tick() function, which is called by the engine's logic update loop using a list of all game objects.
However, I now have more things I want to animate, and was looking in to doing so with as few classes as possible, and definitely not with N+1 classes.
I looked in to saving references to the variable being animated, using the fact that all variables I wanted to animate were floats. However, it appears C# pointers are analogs to the C pointers, and so I cannot save pointers to the values being animated, which appears to be impossible for the CLR in the first place.
The only alternative I could think of is to use reflection to record the argument being passed, then use reflection again to find the value to alter each tick. But then using dozens of reflection calls 60 times a second did not appeal to me.
Since animating objects is a near universal feature in games, I was wondering if there were established practices.