- Home /
Get GameObject transform inside a custom class
Hello community,
I tried to make a script for a turret to fire projectiles and having a difficulty at getting the transform.position of the GameObject inside a custom class.
To clear things, I created a GameObject named BulletPoint, and from that gameobject I wanted to fire projectiles from that gameobject (itself) position and rotation.
public class BulletPoint : MonoBehaviour
{
// Pattern setting class
[Serializeable]
public class PatternSetting
{
// PUBLIC VARIABLES AND OTHER CODES
// ...
// Start firing the bullets
public GameObject Bullet;
public void Fire()
{
Instantiate(Bullet, transform.position, transform.rotation);
}
}
}
The error :
Assets/BulletPoint.cs(36,53): error CS0038: Cannot access a nonstatic member of outer type `UnityEngine.Component' via nested type `BulletPoint.PatternSetting'
I had my reasons why it MUST be in a custom class. This script gives error at the Instantiate line (at transform.position and transform.rotation argument)
I tried using gameObject.transform.position and self with no luck. The only idea my mind comes up is using
public transform SpawnTarget
in my custom class and manually reference it to itself in the inspector, but this is very tedious as this script will be used in numerous prefabs in my game.