у меня есть ошибки в коде, как их исправить?
Assets\Scripts\AllShapesJump.cs(8,34): error CS0027: Keyword 'this' is not available in the current context
Assets\Scripts\AllShapesJump.cs(8,108): error CS0119: 'Rigidbody2D' is a type, which is not valid in the given context
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AllShapesJump : MonoBehaviour
{
private int i = 0;
private Rigidbody2D rbthis = gameObject.transform.GetChild(0).GetChild(0).gameObject.GetComponent(Rigidbody2D);
public void ShapesJump()
{
for (; i < transform.childCount; i++)
{
if (Mathf.Abs(rbthis.velocity.y) < 0.05f)
{
rbthis.AddForce(new Vector2(Random.Range(-25f, 25f), 35f), ForceMode2D.Impulse);
}
}
}
}
Ответы (1 шт):
Автор решения: Алексей Шиманский
→ Ссылка
Строку
private Rigidbody2D rbthis = gameObject.transform.GetChild(0).GetChild(0).gameObject.GetComponent(Rigidbody2D);
сделать
private Rigidbody2D rbthis = null;
А вот в методе Awake или Start уже сделать инициализацию
rbthis = gameObject.transform.GetChild(0).GetChild(0).gameObject.GetComponent(Rigidbody2D);
для решения проблемы с
'Rigidbody2D' is a type, which is not valid in the given context
Надо испльзовать взятие компонента либо так
GetComponent(typeof(Rigidbody2D)) as Rigidbody2D
либо
GetComponent<Rigidbody2D>();
либо
GetComponent("Rigidbody2D") as Rigidbody2D;
Это написано в документации