I have project, inside there is background.cs class and game1.cs class,
thats my background.cs class code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
namespace Rocket
{
class Backgrounds
{
public Texture2D texture;
public Rectangle rectangle;
public void Draw(SpriteBatch spriteBatch){
spriteBatch.Draw(texture, rectangle, Color.White);
}
class Scrolling : Backgrounds{
public Scrolling(Texture2D newTexture, Rectangle newRectangle){
texture = newTexture;
rectangle = newRectangle;
}
public void Update(){
rectangle.X -= 3;
}
}
}
}
and thats game1.cs class code (starting where I get error):
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace Rocket
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Scrolling scrolling1;
Scrolling scrolling2;
'
So, Scrolling scrolling1; is underlined, (as second one), it says that class Scrolling could not be found, but it exists! I am noob in XNA and I cant find why it isn't working. any help will be ok!
Why do you have your Scrolling class nested into Backgrounds class in background.cs? The default visibility for nested classes in C# is private and therefore it is not visible to the Game1 class.
You should put this
class Scrolling : Backgrounds{
public Scrolling(Texture2D newTexture, Rectangle newRectangle){
texture = newTexture;
rectangle = newRectangle;
}
public void Update(){
rectangle.X -= 3;
}
}
into a file scrolling.cs. This gives Scrolling and Background the default visibility for classes (internal) which should work for your example.
You should to specify the visibility of classes explicitly. Have a look at: https://msdn.microsoft.com/en-us/library/ms173121.aspx
Related
I have Example.cs which has attached to the Main Camera, and in the Main Camera's Inspector I just can see the aPoint appear. Both aPoint and bPoint are public but why static makes bPoint doesn't appear in the Inspector.
using System.Collections;
using UnityEngine;
public class Example : MonoBehavior
{
public int aPoint;
public static int bPoint;
}
- error message
After completing the bullet script operation, I tried to insert the script into Bullet Sprite in 2D Object format, but an error was displayed and the insertion was not possible. I tried turning off and on the editor because I thought it was a bug because I had the same file name and class name, but it didn't work. Do you happen to know about this problem? The editor version is 2021.3.11f1.
Bullet Script
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Pool;
public class Bullet : MonoBehaviour
{
[SerializeField]
private float speed = 10f;
private IObjectPool<Bullet> pool;
void Update()
{
transform.Translate(Vector2.up);
}
public void SetPool(IObjectPool<Bullet> bulletPool)
{
Launcher.Instance.bulletPool = pool;
}
private void OnBecameInvisible()
{
Launcher.Instance.bulletPool.Release(this);
}
}
I turned off and on the editor and changed the class name because I thought it was different.
I am trying to make a FlappyBird on WebGL with Unity. I made scores system, and there is no bug finded while runing on unity editor, but when I build with WebGL, Text don't shows up. Only legacy Text (Not TextMeshPro) causes this problm, so it would be helpful too if there is a way to use TextMeshPro. https://r0k0r.github.io/FlappyBirdWebGL is my game link, and https://github.com/R0K0R/FlappyBirdWebGL is my github link. I don't know this will help, but I am currently coding with ubuntu.
this is my Score.cs code that returns current score:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour
{
public static int score = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter2D(){
score++;
}
public static int returnScore(){return score;}
}
and this is my ApplyScore.cs code that applys score to text gameobject.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ApplyScore : MonoBehaviour
{
public Text ScoreText;
// Start is called before the first frame update
void Start()
{
ScoreText.text = "0";
}
// Update is called once per frame
void Update()
{
ScoreText.text = Score.returnScore().ToString();
}
}
this is what it looks like
and this is what it should look like
I've recently discovered the URP and am developing a 2D game for fun. I added a global 2d light and set up some code to grab the light component, but unity keeps saying that light 2D does not exist. The error in unity is "error CS0246: The type or namespace name 'Light2D' could not be found". This is the code I have so far. I also followed a tutorial by Jimmy Vegas to grab the system time.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class DayNight : MonoBehaviour
{
public GameObject theDisplay;
public GameObject GL;
public int hour;
public int minutes;
public int totaltime;
private bool AM;
void Start()
{
totaltime = System.DateTime.Now.Hour;
hour = System.DateTime.Now.Hour;
minutes = System.DateTime.Now.Minute;
}
public void Night()
{
//change light settings
GL.GetComponent<Light2D>().Intensity = 0.4;
}
}
To reference the Light2D Component you need to add the UnityEngine.Experimental.Rendering.Universal namespace to your code.
Namespace Example:
...
using UnityEngine;
using UnityEngine.Experimental.Rendering.Universal;
public class DayNight : MonoBehaviour
{
...
}
If you get an Error you need to just continue and try to compile it, to see if it actually works.
After that is done you should be able to access the Light2D Component of your GameObject without any further troubles.
Calling Light2D Example:
public float nightvalue = 0.4f;
public void Night()
{
// Change light intensity to nightvalue.
GL.GetComponent<Light2D>().intensity = nightvalue;
}
I have made a basic scripted tile, it have set prefab as game object. Problem is, that gameobject is offset when door is placed. I would like to have gameobject on same location as tile.
IMAGE: My problem
DoorTile.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.Tilemaps;
public class DoorTile : TileBase
{
public Sprite doorSprite;
public GameObject doorObject;
public Tile.ColliderType colliderType = Tile.ColliderType.Sprite;
public override void RefreshTile(Vector3Int location, ITilemap tilemap)
{
tilemap.RefreshTile(location);
}
public override void GetTileData(Vector3Int location, ITilemap tilemap, ref TileData tileData)
{
tileData.gameObject = doorObject;
tileData.colliderType = colliderType;
tileData.sprite = doorSprite;
tileData.flags = TileFlags.LockTransform;
tileData.color = Color.white;
}
}
This is due to your Tile Anchor being (0.5, 0.5, 0). You can modify your tile anchor under the Tilemap component to fix this, or you can simply add the offset to your Door object.