x


The best overload for the method 'UnityEngine.Object.FindObjectOfType(System.Type)' is not compatible with the argument list '(s_mainCharacter)'.

I followed a tutorial i am sure i have exactly what he had except for the name but i keep getting this error "Assets/Galaxy_Quest/Scripts/s_HUD.js(12,43): BCE0017: The best overload for the method 'UnityEngine.Object.FindObjectOfType(System.Type)' is not compatible with the argument list '(s_mainCharacter)'." here is my code for both files:

*s_mainCharacter*

    static var SPEED = 7; 
    static var TURBO = (SPEED *2);
    var rotateSpeed = 3.0;
    var bullitPrefab:Transform;

    var pHealth : int;
    var pScore : int;
    var pLife : int;
    var pPower : int;

    function Update () {

    var controller : CharacterController = GetComponent(CharacterController);
    // Rotate around y - axis
    transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
    // Move forward / backward
    var forward = transform.TransformDirection(Vector3.forward);
    var curSpeed = SPEED * Input.GetAxis ("Vertical");
    controller.SimpleMove(forward * curSpeed);

    if(Input.GetButtonDown("Jump")){
        //var bullit = Instantiate(bullitPrefab, GameObject.Find("leftSpawnPoint").transform.position, Quaternion.identity);
        var bullit = Instantiate(bullitPrefab, GameObject.Find("rightSpawnPoint").transform.position, Quaternion.identity);
        bullit.rigidbody.AddForce(transform.forward * 2000);
    }

}

function damageTaken(totalDamage : int){
    pHealth -= totalDamage;
    if(pHealth <= 0){
        if(pLife <=0){
            killPlayer();
        }
            else {
                pLife --;
            }
    }

}

function killPlayer(){
    print("YOU DIED");
}

function addScore(totalScore : int){
    pScore += totalScore;
}

function getStatus(send : int){
    switch (send){
        case 1:
            return pLife;
            break;
        case 2:
            return pHealth;
            break;
        case 3:
            return pPower;
            break;
        case 4:
            return pScore;
            break;          
    }
}

*s_HUD.js*

private var s_mainCharacter : s_mainCharacter;

var playerLife_pos = Vector2(0,0);
var playerHealth_pos = Vector2(0,0);
var playerPower_pso = Vector2(0,0);
var playerScore_pos = Vector2(0,0);

function Update () {
}

***

//I get error in the Awake function everything else works

***
function Awake(){

    s_mainCharacter = FindObjectOfType(s_mainCharacter); 
}

function OnGUI(){
    var playerLife = s_mainCharacter.getStatus(1);
    var playerHealth = s_mainCharacter.getStatus(2);
    var playerPower = s_mainCharacter.getStatus(3);
    var playerScore = s_mainCharacter.getStatus(4);

    GUI.Label(Rect(playerLife_pos.x,playerLife_pos.y,100,100), playerLife.ToString());
    GUI.Label(Rect(playerHealth_pos.x,playerHealth_pos.y,100,100), playerHealth.ToString());
    GUI.Label(Rect(playerPower_pso.x,playerPower_pso.y,100,100), playerScore.ToString());
    GUI.Label(Rect(playerScore_pos.x,playerScore_pos.y,100,100), playerPower.ToString());
}
more ▼

asked Jul 25 '10 at 05:52 PM

Patrick 1 gravatar image

Patrick 1
17 1 1 1

I don't think this is the problem, but it isn't normally a good convention to name a variable the same thing as its type. It just gets confusing like var float : float;

Jul 25 '10 at 06:10 PM Peter G
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You named your variable the same as the class

Change it to something else and it should work

Edit for the sake of clarity:

The error you're getting is because you're trying to pass an instance of the class instead of the class type itself to the function

Example which doesn't work:

class a extends MonoBehaviour
{
    var b : b;

    function Start ()
    {
        b = GetComponent(b);
    }
}

class b
{
}

Example which works:

class a extends MonoBehaviour
{
    var _b : b;

    function Start ()
    {
        _b = GetComponent(b);
    }
}

class b
{
}
more ▼

answered Jul 25 '10 at 06:16 PM

Mike 3 gravatar image

Mike 3
30.5k 10 65 253

Ok, I voted you up, because I figured I may as well test his code, and you were correct. Funny thing is I posted something similar to that as a comment like 15 minutes after he asked the question and he never commented anything back. I said "I don't think that's the problem", but it actually was anyway and if he had read it and tried it, this whole debate would be over. The other thing is that the line number is wrong. He formatted his code before he posted here, because even if you remove the comments, the error is on line 13. So I'm not sure what else he added or removed.

Jul 27 '10 at 12:16 PM Peter G
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1949
x198
x18
x16
x4

asked: Jul 25 '10 at 05:52 PM

Seen: 3457 times

Last Updated: Jul 26 '10 at 09:36 AM