Can't change an object Layers at runtime via c# code (Help Please)
My last question got deleted for some reason so i'm gonna ask again.
I'm trying to change an object's layer at runtime via C# script. But every time I run the game the object's layer comes blank.
EDIT: Yes I already checked if the naming was spelled correct and it is.
Here is my code:
using UnityEngine;
using System.Collections;
public class Player_Movement_Script : MonoBehaviour {
public float movementSpeed;
public float jumpHeight;
public Rigidbody2D rB2D;
public SpriteRenderer SpriteRen;
public bool grounded;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask GroundLayer;
//public LayerMask PlatformLayer;
void Start ()
{
rB2D = GetComponent<Rigidbody2D> ();
SpriteRen = transform.Find("Sprite").GetComponent<SpriteRenderer> ();
GroundLayer = LayerMask.NameToLayer("GroundLayer-sama");
}
void Update ()
{
Movement ();
GroundCheck ();
}
void Movement ()
{
if (Input.GetKey (KeyCode.RightArrow)) {
rB2D.velocity = new Vector2 (movementSpeed, rB2D.velocity.y);
SpriteRen.flipX = false;
}
if (Input.GetKey (KeyCode.LeftArrow)) {
rB2D.velocity = new Vector2 (-movementSpeed, rB2D.velocity.y);
SpriteRen.flipX = true;
}
if(!Input.GetKey (KeyCode.RightArrow) && !Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow) && Input.GetKey(KeyCode.LeftArrow))
{
rB2D.velocity = new Vector2 (0,rB2D.velocity.y);
}
if (grounded) {
if (Input.GetKeyDown (KeyCode.Space)) {
rB2D.velocity = new Vector2 (rB2D.velocity.x, jumpHeight);
}
}
}
void GroundCheck ()
{
grounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, GroundLayer);
}
}
problem.png
(337.7 kB)
Comment
Your answer
Follow this Question
Related Questions
Getting trouble in migrating the Unity IAP old version to new version of IAP in existing project 0 Answers
Bullet destroy 2 game object intent one Unity 2D 0 Answers
Unity 2D rotation not smooth? 0 Answers
Can not load the next scene 0 Answers
Spawn and drag on spawn multiple objects with android multiouch 0 Answers