Answer the question
In order to leave comments, you need to log in
C# Unity - How to initialize movement when colliding with an object?
While creating a game in Unity, I ran into the problem of initializing the movement of one object when colliding with another. The idea is that an object (snake's head) follows the mouse cursor, and when it comes into contact with another object ("food"), it should "eat" it, as it were, by placing it in its tail (it turns out to be a kind of snake). In fact, when the project is launched, the "head" moves, but if you "eat food" - an error pops up - it is attached below and the two scripts that are used. Please do not write off-topic questions or brief incomprehensible answers (such as - NullReferenceException: Object reference not set to an instance of an object - it means something is not assigned somewhere, etc.) - this is a no brainer, that something is not assigned, the path is not specified, but I ask you to provide more detailed help (I am new to c #), poke your nose there,
// Ошибка:
NullReferenceException: Object reference not set to an instance of an object Node.Init (UnityEngine.Transform tr) (at Assets/Node.cs:12)
HeadMovement.OnTriggerEnter2D (UnityEngine.Collider2D col) (at Assets/HeadMovement.cs:76)
// Первый скрипт
using UnityEngine;
public class Node : MonoBehaviour
{
Transform parentNode;
Vector3 destinationPoint;
bool inited;
public void Init(Transform tr)
{
parentNode = tr;
destinationPoint = tr.position; <b>// Строка с ошибкой</b>
GetComponent().color = new Color(1.0f, 0.0f, 0.0f);
inited = true;
}
// Update is called once per frame
void FixedUpdate()
{
if (inited)
{
transform.position = transform.position + (destinationPoint - transform.position) * 5f * Time.fixedDeltaTime;
if ((transform.position - destinationPoint).sqrMagnitude < 0.1f)
destinationPoint = parentNode.position;
}
}
}
// Ниже скрипт из которого и было обращение к скрипту выше (скрипт HeadMovement)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class HeadMovement : MonoBehaviour
{
Vector2 mousePosition;
private float quo;
private float delta;
public float mass;
private Vector2 randVec;
private Vector3 vecScale;
private float camSize;
private int massCoin;
public Camera camer;
Transform lastNode;
[SerializeField]
GameObject foodPrefab;
// Start is called before the first frame update
void Start()
{
mass = 10;
delta = 5;
vecScale.Set(1, 1, 1);
delta = 16 * Mathf.Pow(20, -Mathf.Log(2, 01.1f)) * Mathf.Pow(mass, Mathf.Log(2, 0.1f));
camSize = 4;
massCoin = 5;
}
// Update is called once per frame
void Update()
{
delta = 16 * Mathf.Pow(20, -Mathf.Log(2, 0.1f)) * Mathf.Pow(mass, Mathf.Log(2, 0.1f));
mousePosition = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
mousePosition -= (Vector2)transform.position;
quo = Mathf.Sqrt(mousePosition.x * mousePosition.x + mousePosition.y * mousePosition.y)/ (delta/2);
mousePosition /= quo;
transform.Translate(mousePosition * Time.deltaTime);
vecScale.Set((mass/ 200 + 0.95f), (mass/ 200 + 0.95f), 1);
transform.localScale = vecScale;
mass -= 0.0000002f * mass * mass;
if (camer.orthographicSize > camSize)
{
if (camer.orthographicSize - 1 > camSize)
{
camer.orthographicSize = camSize;
}
else{
camer.orthographicSize -= 0.0001f;
}
}
else if (camer.orthographicSize < camSize)
{
if (camer.orthographicSize + 1 < camSize)
{
camer.orthographicSize = camSize;
}
else{
camer.orthographicSize += 0.0001f;
}
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.gameObject.CompareTag("Food"))
{
if (lastNode == transform)
col.gameObject.tag = "Head";
else
StartCoroutine(SetTagWithDelay(col.transform));
col.transform.GetComponent<Node>().Init(lastNode); <b>// строчка-обращение к скрипту Node</b>
lastNode = col.transform;
}
if (col.gameObject.tag == "Food")
{
mass += massCoin;
randVec.Set(Random.Range(-99.5f, 99.5f), Random.Range(-99.5f, 99.5f));
col.gameObject.transform.position = randVec;
camSize += 0.005f;
}
}
IEnumerator SetTagWithDelay(Transform tr)
{
yield return new WaitForSeconds(0.5f);
tr.gameObject.tag = "Tale";
yield return null;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question