0

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)

Adda
  • 431
  • 4
  • 12
  • Could you please explain what exactly is not working as expected? What happens instead? You seem to know the lines of code that are wrong so .. what exactly is wrong with them in your opinion? – derHugo Jun 17 '19 at 03:07
  • Which error? If you get anything logged to the console please add that to your question. – derHugo Jun 17 '19 at 03:45
  • @derHugo already edit my question. Please do help assist me.. – Adda Jun 17 '19 at 04:03

1 Answers1

1

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).

derHugo
  • 49,310
  • 9
  • 37
  • 73
  • 1
    @Adda nope, I'm sorry but that is absolutely against the community rules. I provided a link how to [debug](https://docs.unity3d.com/Manual/ManagedCodeDebugging.html) your code on runtime .. use it. Simply one of your references is not set .. find out why! Also have a read at [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – derHugo Jun 17 '19 at 04:51