Unity Parent's SetActive() influences child? - unity3d

I discovered that when setting a parent true, that parent's children are all called 'OnEnable' function. Even though children are all set true already.
I don't want to call 'OnEnable' function even though it's already set true.
How can I prevent it?

This documentation on execution order may be useful to you.
If you have logic in the child objects that you do not want to call from the parent being set as active, than remove it from OnEnable and hook it up to your own delegate somewhere else.
You are going to have the same thing happen with the Start and Awake functions as well. Personally I would try and find a way to abstract your code a bit more to make sure the child object is not dependent on the parent.

SetActive() behavior is inherited by all of the children of that particular gameObject. In order to solve your problem you most likely have to restructure your GameObject hierarchy.

Related

Getting a sprite to issue a message when it is removed from the scene after removefromParent?

Is there some way in Swift that I can tell when an SKSpriteNode has actually been removed from the scene? I don't think it's actually done when removeFromParent is called, but instead I think it's done later, when Sprite-Kit thinks it's convenient to do so.
I'm trying to understand the full life cycle and I've noticed that a sprite can still be involved in contact and collisions in didBeginContact even after that sprite has been removed.
If I print out the contents of children (i.e. the array holding all the children of the scene) I see that the sprite is removed as soon as removeFromParent called, but the sprite is still available (at least, for this execution of the SK game loop).
Edit: This question came out from an earlier question of mine concerning didBeginContact being called multiple times for one contact (Sprite-Kit registering multiple collisions for single contact) and discovering that removing the sprite during the first contact did not prevent the subsequent contact(s). (Because SK has 'queued' to contacts in advance.) so I was wondering when the sprite is actually removed.
Am I missing the obvious? So even after removeFromParent the sprite is still around. However, it might well be because I have assigned the node to a temporary SKSpriteNode variable, then as long as that variable is around, there is a strong reference to the node, so it won't be deallocated. Also the SKPhysicsContact object itself will retain a reference to the physicsBody, which has a reference to the node which I think will also prevent allocation.
Update
To see when a sprite is actually released, use the deinit() method:
deinit {
print("Invader of type \(type) deinitialised")
}
I think this can only be added in a subclass definition, not via an extension.
Having a variable with a strong reference to the node being removed will prevent the node from being de-allocated until that variable is itself removed or changed to refer to something else.
If I've understand your question, I think it's not needed because you can always do:
if let myNode = self.childNode(withName: "//myNode") {
// ok myNode exist
}
Hope it helps you, you can write this code wherever you think is necessary.
Update:
About the reformulation of your question take a look below to these comments.
I have a suggestion after reading through the comments... move the node to a place outside of the playable area of your game, then remove it from parent. At this point you don't have to worry about when the physics body gets removed or when SK handles it. Or you could set the physicsBody? to nil at the same time, or use a bitmask flag as KoD suggested.
You can override all of the functions in the SK loop and check to see exactly when your pb is removed: https://developer.apple.com/library/content/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Actions/Actions.html

GameObject isn't in the hieararchy, but its script's Update is called?

Now I am facing a ghost object.
There was a script attached to an object which I deleted. But for some reason, the script's update function is always being called.
I added this line to the Update: Debug.Log(name), and its name is Flamestrike, but when I search it in the hierarchy, there are no results.
And if I set its position to 0,1,0 (so it should be visible), it is not visible in the game either.
So please help me because it drives me crazy :(
Edit:
Debug.Log("a");
if(transform.parent == null) {
transform.SetParent(GameObject.Find("Canvas").transform);
Debug.Log("Canvas");
}
I also tried this, and it prints Canvas, so it sets its parent to Canvas, but I can't see any Flamestrike objects under Canvas.
When Instantiating a prefab GameObject, for some reason it was being created with Hidden Flags. In order to solve this, we added the code: this.gameObject.hideFlag = HideFlags.None; which allowed the object to show up in the hiearchy finally.
The only other source I could find on this was this answer, which had basically no explanation as to why it occured. http://answers.unity3d.com/questions/921819/instantiated-prefabs-not-showing-up-in-hierarchy.html (quoted below)
Found the issue, not sure why but: These two ways are working (prefab
is a GameObject defined elsewhere).
Player1 = Instantiate(prefab) as GameObject;
Instantiate(prefab) as GameObject;
For some reason the prototype:
Instantiate(prefabName, position, rotation);
Was not working.
(Sorry if this is not the correct way to write this, but the comments are too short, and can't be formatted)
This not fully true, I think.
This hidden gameObject wasn't created via script. I dragged the prefab to the scene, then somehow I didn't delete the prefab fully. (or didn't even delete, just made it hidden)
It disappeared in the hieararchy, but it wasn't deleted fully.
After we made the object visible via script, now it is visible.
(but only in play mode, so I can't delete it, because it is not visible in scene view)
And it got some interesting components/attributes.
The original story:
FlameStrike - a Container for scaling purposes (empty gameObject)
-FlameStrike - a gameobject with animation, FlameStrike.cs script etc.
--Particle System - a part. system with this exact name.
But after we found the remaining hidden object it was like this:
So I don't understand what I did. It deleted some parts of the prefab, but also mixed some parts,
(like adding the Particle System object's Particle System component to this FlameStrike object)
then made it hidden. Is there a hotkey for this? :D Because it is not script related issue, I did this in the editor.

how to remove a UIView from the collision behavior in swift

I have been programming a Break Out app for my swift class and I have been able to figure out everything except for how to remove one of the top paddles from the collision behavior. If someone could tell me how to remove something from the collisionBehavior's array of items, that would be great. Thank you.
Set the dynamic flag to false?
The default value is YES. If the value is NO, the physics body ignores all forces and impulses applied to it. This property is ignored on edge-based bodies; they are automatically static.
SKPhysicsBody Class Reference

SWIFT: SKSpriteNode setting alpha to 0 vs. removing it from it's parent

I have a problem and I have to admit, I'm a bit of confused. I have to show/hide some SpriteNodes (those are some option buttons that will appear if the game is over). If the retry "button" (SpriteNode) is pressed by the player, the method that is called by that touch is setting it's alpha to zero. But sometimes it remains "active"(means alpha remains 1), behind other sprites and while the game is played again and the nodes that are in front of it are moving and it becomes visible. That is very confusing and being a random behavior I was thinking if removing it from it's parent (which is self, anyway) and adding it again when it's necessary is a better way to approach that kind of situations.
The code is like:
func restartGame()
{
restartButon.alpha = 0
......................
}
if nodeAtPoint(location).name == "someName"
{
restartGame()
}
It's good to mention that the appearance of this button is made by SKAction.fadeAlphaTo(1,1.5) and it's initially alpha is set to 0 in didMoveToView() override method.
Thanks.
If you still seeing it, it obviously means something in your program is setting the alpha to 1.
Why not just set the node's hidden property to true? This will hide it.
In general, if this is for UI elements which are not always displayed, you are better off by controlling these pieces by a parent node which can contain all of the UI elements. For example, you could have a "menu" node, which contains all of the button nodes. Note this is very simplistic in that more complicated UIs would require a more complicated node hierarchy. But this would work if your UI is layer "floating" over the game.
The advantage of this is you could either remove everything by removing the parent from the tree or just set the parent's hidden property to true

AutoDetectChangesEnabled Behavior

I have a theoretical issue here. I've learnt that DbContextConfiguration.AutoDetectChangesEnabled property value determines whether the automatic detection of changes in the context is enabled. But, I've set it to false and the context still persists changes even when I don't call DbChangeTracker.DetectChanges manually.
Note: the only case when it doesn't persist is when I make changes to ICollection property of an entity (but it still persists when I change a navigation property i.e.: myEntity.OtherEntity = myOtherEntity).
So, I'd like to know what I've learnt wrong :)
Thanks a lot.
Giorgi
It happened because I manually set entity's state to Modified. This made the context save the changes for primitive and navigation properties but apparently wasn't enough for updating the collection property. Problem is solved.