- Home /
How do i reference a path as a sprite?
I created a Gun class with a constructor. One of the properties is a sprite. I made a "Weapons" script to handle all the guns that i create. When i use the new keyword to create a new instance of the class, i need to feed with values and a sprite. And i cant figure out, how to give it a path to a Sprite and use it. I tried something and it doesnt work.
public Gun pistol = new Gun(10, 10f, new Vector2(1.175f, 0.07f), "Pistol",1 , 50, 7, Resources.Load("Assets/Sprites/Pistol") as Sprite);
Ignore all the values except the one with "Resources.Load". There i need to give it a Sprite somehow. Like i mentioned, in the Gun class (It inherits from another class where the property is) is a property of type Sprite. Do i need to change it or is there a way i can create an instance of the class and give it a Sprite without referencing it in the inspector?
public class Gun : Weapon
{
public float FireRate { get; set; }
public float Spread { get; set; }
public Gun(float damage, float rotationSpeed, Vector3 attackPoint, string name, int id, float fireRate,
float spread, Sprite sprite) : base(damage, rotationSpeed, attackPoint, name, id, sprite)
{
Damage = damage;
RotationSpeed = rotationSpeed;
AttackPoint = attackPoint;
Name = name;
ID = id;
FireRate = fireRate;
Spread = spread;
}
}
Answer by Nickshepp · Mar 11, 2021 at 09:28 AM
I'm fairly new to this, but I was told that when using a relative path, it's expecting there to be a Resources folder within Assets.
So maybe try creating that folder, put your sprites folder inside it and using
Resources.Load<Sprite>("Sprites/pistol")
You may need the extension .png, .jpg as well. But the <Sprite>
value is needed I think. Maybe that helps?
Your answer
