我正在做TCG (交易纸牌游戏)项目,并且正在为卡片进行拖动,除了卡片预制件应该返回的父程序外,它都是工作的。在编辑器中,它显示卡片预置返回到主屏幕,即使它应该返回到player1hand区域的原始位置,C#代码中没有显示错误,但是在整体余弦中有一个错误。
下面是编辑器控制台中的error
下面是CardController类
public class CardController : MonoBehaviour, IPointerDownHandler, IPointerUpHandler,IDragHandler {
//creating a special variable with card type
public Card card;
//creating a 2 variables with Image type for the card sprite and playerhand background
public Image character, image;
//creating a 3 variables with TextMeshProUGUI type for the card stats
public TextMeshProUGUI cardName, attack, defence, level, description;
//dont know what to write plz help
private Transform originalParent;
//dont know what to write plz help
private void Awake()
{
image = GetComponent<Image>();
}
//assgining the data from Card class to this class
public void Initialize(Card card)
{
this.card = card;
character.sprite = card.character;
cardName.text = card.cardName;
attack.text = card.attack.ToString();
defence.text = card.defence.ToString();
level.text = card.level.ToString();
description.text = card.description;
originalParent = transform.parent;
}
//what should happen when releasing the card
public void OnPointerUp(PointerEventData eventData)
{
//dont know what to write plz help
image.raycastTarget = true;
//returning the card to his original position
transform.SetParent(originalParent);
//to center the card in the middle of the parent
transform.localPosition = Vector3.zero;
}
//what should happen when holding the card
public void OnPointerDown(PointerEventData eventData)
{
//changing the position of the card when holding left click
transform.SetParent(transform.root);
//dont know what to write plz help
image.raycastTarget = false;
}
//what should happen when dragging the card
public void OnDrag(PointerEventData eventData)
{
//changing the position of the card to the new position of the mouse while holding left click
transform.position = eventData.position;
}}这是CardManager类
public class CardManager : MonoBehaviour {
// making CardManager class gobal to access
public static CardManager instance;
// to create a list of cards
public List<Card> cards = new List<Card>();
// to declare the place where cards are summoned
public Transform player1Hand, player2Hand;
// creating a cardControllerPrefab varible to hold the CardPrefab from the editor
public CardController cardControllerPrefab;
//summoning CardController class in CardManager class
private void Awake()
{
instance = this;
}
//to start the GenerateCard function when the game starts
private void Start()
{
GenerateCards();
}
//to generate the cards using foreach loop and putting them in place
private void GenerateCards()
{
foreach(Card card in cards)
{
//to create a new card using CardController class and putting it in CardControllerPrefab
CardController newCard = Instantiate(cardControllerPrefab, player1Hand);
//to put the card in the player1Hand Area
newCard.transform.localPosition = Vector3.zero;
//to actavite the Card data class using a code in the CardController class
newCard.Initialize(card);
}
}}如果你也能帮我改进我的评论,我会很感激的
发布于 2022-07-26 11:28:38
当Unity遇到异常时,它会在当前方法上运行,但在其他情况下将继续运行(而不是崩溃到桌面)。
您还没有告诉我们第25行的内容,但是它阻止了您的代码进入下面一行:
originalParent = transform.parent;您不能设置它(因为当您在第25行上命中错误时会中止),所以当您使用它时它是空的。但是,空转换父函数是一个完全有效的选项--它意味着转换没有父级(并且应该是根级游戏对象)。
最好的建议是修复空引用异常,但如果做不到这一点,就可以将originalParent行移到Initialize方法的顶部。
发布于 2022-07-26 08:00:27
Unity获得父对象:
GameObject fu = this.transform.parent.gameObject; //Get the parent object of the current object
GameObject fufu = fu.transform.parent.gameObject;// Get the parent object of the specified object希望它能帮到你。
https://stackoverflow.com/questions/73098955
复制相似问题