Unity check if scene == gamescene? - unity3d

I'm a bit new to Unity, so im asking for you guys help.
How can i check if the Scene == "gamescene"?
I tried something like this:
if(SceneManager.loadedScene == "gamescene") { do something }
I want to do an auto save only if the opened scene is the "gamescene".
But i haven't seen it working..

Your logic is right, but you should use SceneManager.GetActiveScene().name.

Your need to use SceneManager.GetActiveScene() then get the name of the scene from it.
if (SceneManager.GetActiveScene().name == "gamescene") { }

Related

app crashing after trying to load an skspritenode twice?

I asked a question before and i wound up deleting it as i though i figured it out, but i actually just narrowed it down.
When one of my nodes hits into an obstacle, the game presents a score label and a restart button. But if my node hits a different object after the initial hit, the app crashes. If i run the game with // in front of the the label and restart to take them out of the equation the game runs fine, but if i run the game with them, it crashes.
This leads me to believe its crashing because its trying to load the restart button and score label twice. I might be totally wrong but how can i correct this?
I think you're probably right.
Trying to addchild is how I presume you're adding things to the scene. If you try to add something to a scene or object that's already added to a something, you get a crash.
So you're probably trying to add your restart and score label to something, as a child, when they already have a parent, and this is causing the crash.
SOLVED IT!
I added if statements to make the app only run my collision code after 1 collision and not 2
var collision = Int()
func didBegin(_ contact: SKPhysicsContact) {
var firstBody : SKPhysicsBody = contact.bodyA
var secondBody : SKPhysicsBody = contact.bodyB
if collision == 0{
if ((firstBody.categoryBitMask == physicsCatagory.bird) && (secondBody.categoryBitMask == physicsCatagory.obstacle)) {
collisionWithObstacle(bird: firstBody.node as! SKSpriteNode)
}
}
else if collision != 0{
if ((firstBody.categoryBitMask == physicsCatagory.bird) && (secondBody.categoryBitMask == physicsCatagory.obstacle)) {
}
}
}
func collisionWithObstacle(bird:SKSpriteNode){
collision = collision+1
scoreTimer.invalidate()
obstacleTimer.invalidate()
addChild(restart)
scoreLabel2.text = "Score: \(score1)"
addChild(scoreLabel2)
}
hope this can help someone else because although it seems trivial in hindsight, it was a head scratcher for a few nights after work.

how to set orientation for each scene in unity3d

I have 4 scenes in my game
I need to set scene 1 and scene 3 in landscape mode, scene 2 and scene 4 in portrait mode
how can I do this?
please help me out...
I'm searching for a long time but couldn't find out the answer
You can have a script that checks for the actual level and changes the orientation in according to the retrieved value.
As an example:
function Start(){
if (Application.LoadedLevel == 1)
Screen.orientation = ScreenOrientation.LandscapeLeft;
else if (Application.LoadedLevel == 2)
Screen.orientation = ScreenOrientation.Portrait;
//...
}
More info about usage and orientation values.
Since around Unity 5.3 there is a new API for getting the current scene.
This example forces the screen orientation on mobile devices to portrait on a scene called "GameView" and landscape otherwise.
using UnityEngine;
using UnityEngine.SceneManagement;
public class ScreenOrientationScript : MonoBehaviour {
void Start () {
if (Application.isMobilePlatform == true) {
if (SceneManager.GetActiveScene().name == "GameView") {
Screen.orientation = ScreenOrientation.Portrait;
} else {
Screen.orientation = ScreenOrientation.Landscape;
}
}
}
}
Sample iOS setting to allow orientations
Unity uses single/sole activity for ALL Levels/Scenes, which is com.unity3d.player.UnityPlayerActivity, as can be found in AndroidManifest.xml file.
So all you need do is to specify the orientation in the Player Setting Inspector (Default Orientation part):

How can I make images that act as buttons in Swift?

I have the code for one button on my scene, but I tried using that same code to make another button in the same scene, and it didn't work. Here's the code I have so far:
for touch:AnyObject in touches {
let location = touch.locationInNode(self)
if playGameButton.containsPoint(location) {
let newScene = GameplayScene(size: self.size)
newScene.scaleMode = scaleMode
self.view?.presentScene(newScene)
}
}
Again, that's only one button. When I use that same bit of code for another image, it doesn't work.
A better way to approach this problem would be to use an SKSpriteNode object as your button and set its name property to easily distinguish between multiple buttons. For example, if you have an SKSpriteNode object button1:
button1.name = "first"
Then in your touch method to check which was touched, you can use the name property:
SKNode node = self.nodeAtPoint(location)
if node.name == "first" {
// do something
}

Unable to pass a variable to a scene in spritekit in swift

I'm currently having trouble with passing variables to a scene in spritekit (swift). In android when moving to a different activity, I just set the parameter to the Intent and simply get the variable. But since I'm new to spritekit and swift, i find it difficult. Is there a clear way to pass a variable to the scene ? I have a sample code below that I tested but did not work.
class GameSKView: SKView {
var mylevel = 0
}
//Inside my main GameScen
let gameView = self.view as GameSKView
sample.text = "my level is :\(gameView.mylevel)"
Try this:
class GameSKView: SKView {
internal var mylevel = 0
}
And inside you main game scene
if let gameView = self.view as GameSKView {
sample.text = "my level is :\(gameView!.mylevel)"
}
Finally found the solution.
In order to use the custom SKView, I have done the following procedures.
Open the Idenity inspector in the Main.storyboard
Replace the [SKView] inside the Custom Class with your [customSKView]
, In my case GameSKView
Save. That was all I needed to do.

How do I call a function from another scene?

My game unlocks a level every-time they beat a level. So the problem im having is when the user beats level one I want it to show a button of level 2 thats in my GameScene. When I call the function unlockLevelTwo() in my Level1.swift file it doesn't show up in my GameScene.swift file. What am I doing wrong?
//GameScene.Swift
func unlockLevelTwo() {
let fadeIn = SKAction.fadeInWithDuration(0.5)
levelTwo.position = CGPointMake(self.size.width / 2.0, self.size.height / 2.2)
levelTwo.zPosition = 20
levelTwo.setScale(0.8)
levelTwo.alpha = 0
levelTwo.runAction(fadeIn)
levelTwo.name = "leveltwo"
addChild(levelTwo)
}
//Level1.swift
if firstBody.categoryBitMask == HeroCategory && sixthBody.categoryBitMask == GoldKeyCategory{
//calling this function from my gameScene to unlock levelTwo button.
var gameScene = GameScene()
gameScene.unlockLevelTwo()
}
This function:
if firstBody.categoryBitMask == HeroCategory && sixthBody.categoryBitMask == GoldKeyCategory{
//calling this function from my gameScene to unlock levelTwo button.
var gameScene = GameScene()
gameScene.unlockLevelTwo()
}
is creating a new GameScene object and adding the childNode "levelTwo" to that gamescene. Instead, you need to call unlockLevelTwo() on the actual GameScene that is currently presented to the user.
I imagine somewhere in Level1.Swift there is a reference to the current GameScene (the one the user is interacting with)? Call the function on that one.
EDIT:
In essence, what you must do is keep a reference to your original GameScene object somewhere in your code, which I will henceforth refer to it as MyScene. That way, when in Level1.swift, you can reference MyScene and add buttons, levels, whatever you like to it without creating a new one like you did here:
var gameScene = GameScene()
gameScene.unlockLevelTwo()
Instead, you would just call
MyScene.unlockLevelTwo().
If Level1 is a view of some sort, or an object that gets created, in the init function you could pass in your GameScene object and set it like so
class Level1 : (Type) {
var myScene: GameScene!
init(myScene: GameScene) {
self.myScene = myScene)
}
}
Something like that, hope it helps!