0

UnassignedReferenceException: The variable segmentPrefab of Snake has not been assigned. You probably need to assign the segmentPrefab variable of the Snake script in the inspector.

Im making a very simple snake game where you eat the fruit and the snake multiplies and so on, when the snake eats the fruit it creates a segmentprefab clone creates but then i get this error and have no idea how to fix it? please help

  • What exactly do you not understand in the error message? Basically this is a wrapper exception for an exposed reference field in the Inspector that is unassigned ... – derHugo Nov 03 '21 at 16:00
  • That error is fixed now but another problem has happend, when i eat the food a block creates but when i change direction it dissapears. im following this https://www.youtube.com/watch?v=U8gUnpeaMbQ – Andre hopcroft Nov 08 '21 at 14:23

1 Answers1

0

You have to assign a prefab to the segmentPrefab variable before you can clone something. Just select the GameObject on which the Snake script is attached, then in the inspector locate the Snake component and look at the "Segment Prefab" property, it should be empty. Just drag and drop a valid prefab on the field, it should be enough.

Reference: https://docs.unity3d.com/Manual/UsingComponents.html

Daniels118
  • 1,149
  • 1
  • 8
  • 17
  • it isnt empty it says snakeSegment (Transform) – Andre hopcroft Nov 03 '21 at 17:09
  • Okay a bit more context i was following a video https://www.youtube.com/watch?v=U8gUnpeaMbQ and the part at 37:19 isnt working for me – Andre hopcroft Nov 03 '21 at 17:29
  • Have you removed the Snake component from the GameObject before creating the SnakeSegment prefab? He does this at 31:42. If you didn't this then you have a Snake component on your prefab with the Segment Prefab property unset. The Snake component must be placed only on the main segment, not the ones added further. Check your prefab and eventually remove its Snake component. – Daniels118 Nov 03 '21 at 17:42
  • Im sorry im new by GameObject do you mean the scene on the hierarchy if so then yes i removed it and moved to the assets then added it to the snake GameObject. This is the code that controlls how it grows. private void Grow () { Transform segment = Instantiate(this.segmentPrefab); segment.position = _segments[_segments.Count - 1].position; _segments.Add(segment); } private void OnTriggerEnter2D(Collider2D other) { if (other.tag == "Food") { Grow(); } } – Andre hopcroft Nov 03 '21 at 21:33
  • The segmentprefab clones come on the hierarchy when i eat the food but when i change direction they disappear – Andre hopcroft Nov 03 '21 at 21:37
  • Yes, gameobjects are the items in the hierarchy. So, if segments are created successfully when you eat food, when the error happens? – Daniels118 Nov 03 '21 at 22:06
  • is there a way i can show you what happens cant really explain – Andre hopcroft Nov 03 '21 at 23:56
  • do you have discord or something – Andre hopcroft Nov 03 '21 at 23:56
  • or can we use the unity collab option – Andre hopcroft Nov 04 '21 at 00:02
  • As a general rule on stackoverflow you should make the code available to everybody to get help. However feel free to share your project with me using unity collab, my nickname is Daniels118 – Daniels118 Nov 05 '21 at 07:54
  • I need your email to add you – Andre hopcroft Nov 07 '21 at 20:38