Hey Everybody!
So I keep getting these errors
An object reference is required to access non-static member UnityEngine.Component.transform' The best overloaded method match for UnityEngine.Vector3.Distance(UnityEngine.Vector3, UnityEngine.Vector3)’ has some invalid arguments
Argument #2' cannot convert object’ expression to type `UnityEngine.Vector3’
And I can’t figure out why…
If You could help me and figure out why… That would be gerat!
Thank you so much and have a great day
using UnityEngine;
using System.Collections;
public class FireBall : MonoBehaviour {
public static float distance;
public float Speed;
public static FireBall GameObject;
public static Transform Player;
Here’s my script
void start()
{
Player = GameObject.FindWithTag("Player").transform; // finds the Player that has the tag Player
FireBall = GameObject.FindWithTag("FireBall").transform; // finds the Player that has the tag Player
}
void Update ()
{
if (PlayerAttributes.Abilities = true)
{
if(Input.GetKeyDown(KeyCode.Mouse0))
{
transform.position += transform.forward * Speed * Time.deltaTime;
distance = Vector3.Distance (Player.transform.position, FireBall.transform.position);
}
}
}
}
3 Answers
3
First off: Rename your void start() method to void Start(). The Unity methods are case-sensitive, and if they aren’t as expected they simply won’t be called by the engine.
Now, notice on line 8 that you’ve declared a static field named “GameObject” that is of type FireBall (same as your MonoBehaviour). I believe you intended to do the opposite: declare a static field named “FireBall” (which the compiler will not allow, so name it “FireBallObject” instead) of type GameObject.
So something like this should compile:
using UnityEngine;
using System.Collections;
public class FireBall : MonoBehaviour
{
public static float distance;
public float Speed;
public static GameObject FireBallObject;
public static Transform PlayerTransform;
void Start()
{
PlayerTransform = GameObject.FindWithTag("Player").transform; // find a GameObject tagged "Player" and gets its transform
FireBallObject = GameObject.FindWithTag("FireBall"); // find a GameObject tagged "FireBall"
}
void Update ()
{
if(PlayerAttributes.Abilities = true)
{
if(Input.GetKeyDown(KeyCode.Mouse0))
{
transform.position += transform.forward * Speed * Time.deltaTime;
distance = Vector3.Distance(PlayerTransform.position, FireBallObject.transform.position);
}
}
}
}
Depending on your intention and setup I might question your use of statics, but that would be a different issue.
Your big problem is on line 8. You have:
public static FireBall GameObject;
But given your code below the line should be:
public static Transform FireBall;
But that introduces another problem. You cannot have a variable with the same name as the class, so you need to either change the name of your class or change the name of the variable. Note by convention, variable names start with a lower case letter. Class and Function names start with a upper case letter.
The problem you have is:
public static FireBall GameObject;
public static Transform Player;
...
distance = Vector3.Distance (Player.transform.position, FireBall.transform.position);
You are trying to find the transform component of the Transform object. So instead you should write something like Player.position
And I suggest you to remove that static statements, they’re not needed in this case.
Okay thank you :D!
– SirMcsquizy@SirMcsquizy I HIGHLY recommend always always starting variable names with lower case letters, and Classes with upper case letters. That could have prevented a case like this where you defined a variable named GameObject of type FireBall, leading to a very strange error indeed.
– flaviusxvii@flaviusxvii Ya I will keep that in mind... So I just retested out my script... and Im teleporting... What I would like is the fireball to appear and make it go... What else should I do... (I forgot to mention I just started to learn c#... Ive been doing this for a month.. So I'm trying to get the hang of everything
– SirMcsquizyMoving things over time involves Vector3.MoveTowards(), Vector3.Lerp(), or a few other calculations. This is a common question with lots of answers posted on UA. Since this is a new question, if you get stuck, post a new question along with detailed description of your desired behavior.
– robertbu