- Home /
hi, i dont understand this error.
Assets/MoveRight.cs(113,20): error CS0411: The type arguments for method `UnityEngine.GameObject.GetComponent()' cannot be inferred from the usage. Try specifying the type arguments explicitly
in this script
using UnityEngine; using System.Collections;
public class MoveRight : MonoBehaviour { public Vector3 moveSpeed = new Vector3(); private bool moving = false; private GameObject[] scene; private GameObject bg; public AudioClip completeSound; private GameObject[] buttons; private GameObject completeText; private bool ended = false; public Font goodDog;
//Use this for initialization
void Start()
{
scene = GameObject.FindGameObjectsWithTag("Moveable");
bg = GameObject.Find("Background");
buttons = GameObject.FindGameObjectsWithTag("Buttons");
}
// Update is called once per frame
void Update()
{
if (Application.platform == RuntimePlatform.Android)
{
if (Input.touchCount > 0)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
CheckTouch(Input.GetTouch(0).position, "began");
} else if (Input.GetTouch(0).phase == TouchPhase.Ended)
{
CheckTouch(Input.GetTouch(0).position, "ended");
}
}
}
if (Application.platform == RuntimePlatform.OSXEditor)
{
if (Input.GetMouseButtonDown(0))
{
CheckTouch(Input.mousePosition, "began");
}
if (Input.GetMouseButtonUp(0))
{
CheckTouch(Input.mousePosition, "ended");
}
}
// Move if button is pressed && stage is not over
if (moving && bg.transform.position.x > -4.8f)
{
for (int i = 0; i < scene.Length; i++)
{
if (scene [i] != null)
{
scene [i].transform.position -= moveSpeed;
}
}
}
// Stage Completed
if (bg.transform.position.x <= -4.8f && ended == false)
{
Alert("complete");
}
}
void CheckTouch(Vector3 pos, string phase)
{
Vector3 wp = Camera.main.ScreenToWorldPoint(pos);
Vector2 touchPos = new Vector2(wp.x, wp.y);
Collider2D hit = Physics2D.OverlapPoint(touchPos);
if (hit.gameObject.name == "RightButton" && hit && phase == "began")
{
moving = true;
}
if (hit.gameObject.name == "RightButton" && hit && phase == "ended")
{
moving = false;
}
}
public void Alert(string action)
{
ended = true;
completeText = new GameObject();
completeText.AddComponent("GUIText");
completeText.guiText.font = goodDog;
completeText.guiText.fontSize = 50;
completeText.guiText.color = new Color(255, 0, 0);
if (action == "complete")
{
AudioSource.PlayClipAtPoint(completeSound, transform.position);
completeText.guiText.text = "Level Complete!";
completeText.guiText.transform.position = new Vector3(0.24f, 0.88f, 0);
} else
{
completeText.guiText.text = "Game Over";
completeText.guiText.transform.position = new Vector3(0.36f, 0.88f, 0);
}
bg.GetComponent ().stop ();
for(int i = 0; i < buttons.Length; i++)
{
buttons[i].renderer.enabled = false;
Invoke("restart", 2);
}
}
void restart()
{
Application.LoadLevel(Application.loadedLevel);
}
}
Answer by tanoshimi · Apr 05, 2015 at 07:54 PM
Pretty much exactly what the error says... what component are you trying to stop in this line? An audio source? An animation?
bg.GetComponent ().stop ();
Use the generic version of GetComponent and supply the correct type. E.g.
bg.GetComponent<AudioSource>().Stop();
(Also note correct capitalisation of Stop() method)
Answer by codemaker2015 · Jul 09, 2020 at 08:48 AM
Use the GetComponent method with type.
//gameObject.GetComponent<type>()
gameObject.GetComponent<AudioSource>().Stop();