Reputation: 527
I want to make drag and drop games. The game can run and play but there is some error. Based on 2 coding below there's an error:-
Full coding:-
Vector3 startposition;
getTarget.transform.position = Vector3.Lerp(getTarget.transform.position, startposition, 1.0f);
startposition = getTarget.GetComponent<Drag>().originalPosition;
1) Error coding on line 80
startposition = getTarget.GetComponent<Drag>().originalPosition;
Full coding:-
if ((dropobject.name == namepartial+"Target") && (getTarget.name == namepartial))
{
startTime = Time.time;
journeyLength = Vector3.Distance(getTarget.transform.position, dropobject.transform.position);
correct = true;
correctdone = false;
if (dropobject.name == "ToasterTarget")
{
infoPanel1.SetActive(true);
infoPanel2.SetActive(false);
infoPanel3.SetActive(false);
infoPanel4.SetActive(false);
infoPanel5.SetActive(false);
infoPanel6.SetActive(false);
infoPanel7.SetActive(false);
}
}
if ((dropobject.name == namepartial+"Target") && (getTarget.name == namepartial))
{
playAudioCorrect();
target.GetComponentInChildren<Renderer>().enabled = false;
getTarget.tag = "Untagged";
int tempscore = int.Parse(scoretext.GetComponent<Text>().text) + 50;
scoretext.GetComponent<Text>().text = tempscore.ToString();
int tempscore1 = int.Parse(finalScoreText.GetComponent<Text>().text) + 50;
finalScoreText.GetComponent<Text>().text = tempscore1.ToString();
}
2) Error coding on line 150:-
if ((dropobject.name == namepartial+"Target") && (getTarget.name == namepartial))
Error from console at line 80 and 105:-
NullReferenceException: Object reference not set to an instance of an object Drag.Update () (at Assets/GameApaNi/Scripts/Drag.cs:105)
Upvotes: 0
Views: 61
Reputation: 90759
You didn't show which error you actually get but given your code I can only assume that you get two
NullReferenceException
The first means that either getTarget
is not set (null
) - which can not be the case since otherwise you would get the error already in the lines before - or the getTarget.GetComponent<Drag>()
returns null
meaning there is no component Drag
on the getTarget
.
If it is not attached to exactly the getTarget
object you can either use GetComponentInParent<Drag>()
to search it recursively in the hierarchy upwards or use GetComponentInChildren<Drag>(true)
to search for it recursively in the hierarchy downwards.
The second is pretty much the same: Either dropobject
or getTarget
is not set so null
.
For both you should check your Inspector references and set breakpoints and Debug your code line by line.
Some further tips:
In general why are you using exactly the same if
condition
if ((dropobject.name == namepartial+"Target") && (getTarget.name == namepartial))
twice as separated calls? Couldn't you merge the two blocks together into one?
Also avoid your repeated calls of GetComponent
as far as possible do them only once store the results and re-use the references e.g.
var scoreText = scoretext.GetComponent<Text>();
int tempscore = int.Parse(scoreText.text) + 50;
scoreText.text = tempscore.ToString();
If anyhow possible don't even use this but get the according component already only once in Awake
and reuse the same reference for the entire runtime of your app.
If you reference those e.g. via the Unity Inspector in fields like
public GameObject scoretext;
then rather change the type to
public Text scoretext;
and you can completely get rid of all the GetComponent
calls which would make your script way more efficient (fast).
Upvotes: 1