C
C#5d ago
Readable

CS0120 Error Code help

Hey, I am very new to code and Im trying to understand inheritance but while testing my code I got this error and I cant figure out how to fix it. Please help me if you know whats wrong. Its specifically on the shoot function line btw. Line 27 and 28
10 Replies
ero
ero5d ago
Please format your code properly (including the indentations and removing unnecessary empty lines...) and actually show the error and where it is. $code
MODiX
MODiX5d ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Readable
ReadableOP5d ago
// using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour
{
class Gun{
public Rigidbody projectile;
public float speed = 25;
// Update is called once per frame
public void Update()
{
if (Input.GetMouseButtonDown(0))
{
shoot();
}
}
public void shoot()
{
Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
}
}
class Throw : Weapon
{
[SerializeField] private Transform targetCube;
[SerializeField] private float force;
[SerializeField] private float rotationForce;
private Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
if (Input.GetKey(KeyCode.E))
{
Vector3 direction = targetCube.position - rb.position;
direction.Normalize();
Vector3 rotationAmount = Vector3.Cross(transform.forward, direction);
rb.angularVelocity = rotationAmount * rotationForce;
rb.velocity = transform.forward * force;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Enemy"))
{
Destroy(gameObject);
}
}
}
}
// using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour
{
class Gun{
public Rigidbody projectile;
public float speed = 25;
// Update is called once per frame
public void Update()
{
if (Input.GetMouseButtonDown(0))
{
shoot();
}
}
public void shoot()
{
Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
}
}
class Throw : Weapon
{
[SerializeField] private Transform targetCube;
[SerializeField] private float force;
[SerializeField] private float rotationForce;
private Rigidbody rb;
private void Start()
{
rb = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
if (Input.GetKey(KeyCode.E))
{
Vector3 direction = targetCube.position - rb.position;
direction.Normalize();
Vector3 rotationAmount = Vector3.Cross(transform.forward, direction);
rb.angularVelocity = rotationAmount * rotationForce;
rb.velocity = transform.forward * force;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Enemy"))
{
Destroy(gameObject);
}
}
}
}
Pobiega
Pobiega5d ago
public class Weapon : MonoBehaviour
{
class Gun
{
public Rigidbody projectile;
public float speed = 25;

// Update is called once per frame
public void Update()
{
if (Input.GetMouseButtonDown(0))
{
shoot();
}
}

public void shoot()
{
Rigidbody instantiatedProjectile =
Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
}
}

class Throw : Weapon
{
[SerializeField] private Transform targetCube;
[SerializeField] private float force;
[SerializeField] private float rotationForce;
private Rigidbody rb;

private void Start()
{
rb = GetComponent<Rigidbody>();
}

private void FixedUpdate()
{
if (Input.GetKey(KeyCode.E))
{
Vector3 direction = targetCube.position - rb.position;
direction.Normalize();
Vector3 rotationAmount = Vector3.Cross(transform.forward, direction);
rb.angularVelocity = rotationAmount * rotationForce;
rb.velocity = transform.forward * force;
}
}

private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Enemy"))
{
Destroy(gameObject);
}
}
}
}
public class Weapon : MonoBehaviour
{
class Gun
{
public Rigidbody projectile;
public float speed = 25;

// Update is called once per frame
public void Update()
{
if (Input.GetMouseButtonDown(0))
{
shoot();
}
}

public void shoot()
{
Rigidbody instantiatedProjectile =
Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
}
}

class Throw : Weapon
{
[SerializeField] private Transform targetCube;
[SerializeField] private float force;
[SerializeField] private float rotationForce;
private Rigidbody rb;

private void Start()
{
rb = GetComponent<Rigidbody>();
}

private void FixedUpdate()
{
if (Input.GetKey(KeyCode.E))
{
Vector3 direction = targetCube.position - rb.position;
direction.Normalize();
Vector3 rotationAmount = Vector3.Cross(transform.forward, direction);
rb.angularVelocity = rotationAmount * rotationForce;
rb.velocity = transform.forward * force;
}
}

private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Enemy"))
{
Destroy(gameObject);
}
}
}
}
now with readable linebreaks and indentation :p post a screenshot of the compiler errors, including the details
Readable
ReadableOP5d ago
No description
Readable
ReadableOP5d ago
idk why its so small
Pobiega
Pobiega5d ago
and what exactly are lines 18 and 19? the error indicates you are trying to access transform statically, but I can't see any code that actually does that
Readable
ReadableOP5d ago
It’s at the shoot function
ero
ero5d ago
Gun isn't a Component or a MonoBehaviour, so it doesn't know of a transform member. it tries to access transform from the Weapon class that Gun is nested in. but since accessing transform (which is an instance member) requires a valid instance of Component to be present (which would be this in Weapon), you cannot access that member. what you probably wanted to do is class Gun : Weapon, like you did below with class Throw : Weapon but you should not be nesting classes in this way in the first place
Readable
ReadableOP5d ago
Thank you, its working

Did you find this page helpful?