I want to alternate between gameobjects by using transforms in c#
I have an error CS0176 for some reason and I don't understand why plz help thanks.
code
using UnityEngine; using System.Collections;
public class MoveMent : MonoBehaviour {
public float moveSpeed;
public float jumpHeight;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool grounded;
private bool doubleJumped;
// Game Objects/Transforms I want to alternate.
public GameObject FirePoint;
public GameObject FirePoint2;
public Transform firePoint;
private SpriteRenderer mySpriteRenderer;
public GameObject bullet;
// Use this for initialization
void Start () {
}
void FixedUpdate() {
grounded = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, whatIsGround);
}
void Update () {
if (grounded)
doubleJumped = false;
if (Input.GetKeyDown (KeyCode.UpArrow) && grounded) {
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpHeight); }
if (Input.GetKeyDown (KeyCode.UpArrow) && !doubleJumped && !grounded) {
GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jumpHeight);
doubleJumped = true; }
if (Input.GetKey (KeyCode.LeftArrow)) {
GetComponent<Rigidbody2D>().velocity = new Vector2(-moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
}
if (Input.GetKey (KeyCode.RightArrow)) {
GetComponent<Rigidbody2D> ().velocity = new Vector2 (moveSpeed, GetComponent<Rigidbody2D> ().velocity.y);
}
if (Input.GetKey (KeyCode.LeftArrow)) {
mySpriteRenderer.flipX = true;
}
if (Input.GetKey (KeyCode.RightArrow)) {
mySpriteRenderer.flipX = false;
}
if (Input.GetKeyDown(KeyCode.Space))
{
Instantiate (bullet, firePoint.position, firePoint.rotation);
}
// Error Here, <---------------------------->
if (Input.GetKey (KeyCode.LeftArrow)) {
Transform firePoint = gameObject.Find("FirePoint2");
}
if (Input.GetKey (KeyCode.RightArrow)){
Transform firePoint = gameObject.Find("FirePoint");
}
// <-----------------------------------> End of error
}
private void Awake(){
mySpriteRenderer = GetComponent<SpriteRenderer>();
}
}
CS0176: Static member 'member' cannot be accessed with an instance reference; qualify it with a type name ins$$anonymous$$d (https://msdn.microsoft.com/en-us/library/zhcxt2bd.aspx),
So, you're likely trying to call a class function or value (defined as Static with respect to the class itself) using an instance of that class.
Given this, is the error being called at:
gameObject.Find("FirePoint");
If so, this needs to be GameObject (caps) because you need to call the class name.
Also, you already defined "firePoint" in this scope, so there's no need to write:
Transform firePoint = gameObject.Find("FirePoint");
delete "Transform"
Answer by CodingNoob_ · May 27, 2016 at 07:32 PM
Thanks @TreyH I finally fixed it with trial and error :)
Your answer

Follow this Question
Related Questions
Cant edit anything. 0 Answers
HELP!! I don't know how to do the 2Dcharacter jump only once in C#code 0 Answers
Jump Script 2 Answers
Aspect ratio help android 0 Answers
Adding sprites 0 Answers