- Home /
how to call a method from another script?,Does not contain a definition for and no accessible extension method accepting a first argument errors
I have looked almost 10 different topics but nothing worked for me.
I have such code
updateScore.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class updateScoreText : MonoBehaviour
{
Text scoreText;
// Start is called before the first frame update
void Start()
{
scoreText = GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
}
public void scoreUpdate(int score)
{
scoreText.text = "Score: " + score;
}
}
I want to call scoreUpdate method in another script
I tried
public updateScore addScoreText; addScoreText = GameObject.Find("Text").GetComponent().scoreUpdate(points);
in another script
but it shows error error CS1061: 'updateScore' does not contain a definition for 'scoreUpdate' and no accessible extension method 'scoreUpdate' accepting a first argument of type 'updateScore' could be found (are you missing a using directive or an assembly reference?)
,This is my full error : error CS1061: 'updateScore' does not contain a definition for 'scoreUpdate' and no accessible extension method 'scoreUpdate' accepting a first argument of type 'updateScore' could be found (are you missing a using directive or an assembly reference?)
I have two different scripts
PlayerPoint.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class playerPoint : MonoBehaviour
{
public int points = 0;
float timers = 0.0f;
public int seconds = 0;
bool scoreUpdated = false;
public updateScore addScoreText;
// Start is called before the first frame update
void Start()
{
InvokeRepeating("addPoint",5.0f,5.0f);
}
// Update is called once per frame
void Update()
{
}
void addPoint()
{
if (scoreUpdated == false)
{
points += 5;
addScoreText.scoreUpdate(points);
}
}
}
updateScore.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class updateScoreText : MonoBehaviour
{
Text scoreText;
// Start is called before the first frame update
void Start()
{
scoreText = GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
}
public void scoreUpdate(int score)
{
scoreText.text = "Score: " + score;
}
}
Please could someone help me why I get this error when I already have scoreUpdate method in updateScore.cs file?
Answer by hosap4ik · Jun 25, 2019 at 10:00 AM
Silly question.
Solved finally.
I had two reasons.
First one is the class name should be same as file name.
Second one is I didn't include updateScore script file to Text Object.
Your answer
Follow this Question
Related Questions
How can i set transform.position on y ? And why i'm getting error cannot convert double to float ? 3 Answers
unity load an player made C# script 2 Answers
Getting Null Reference Exception on working Script ? 0 Answers
Guys i really need your help! Why i cant use unity addons? 0 Answers
[C#] Quaternion Rotations with Input.GetAxis problems. 1 Answer