C
C#2y ago
henke

✅ Need help with C# unity game development.

The issue I have is that im getting reference null on this line: itemParentObjectTransform.transform.parent = playerController.transform;
//
//In my PlayerController : MonoBehaviour
if(Input.GetKeyDown(KeyCode.B))
{

GameObject[] itemObjects = GameObject.FindGameObjectsWithTag("Items");
foreach(GameObject itemObject in itemObjects)
{
// Retrieve the script component attached to the child object
ItemObject script = itemObject.GetComponentInParent<ItemObject>();
if(script != null)
{
// Call the function on the script
script.PickupItem();
}
}


//In my child of another GameObject::
public class ItemObject : MonoBehaviour
{
public PlayerController playerController;
public GameObject itemObject;
public Transform itemParentObjectTransform;
// bool Pickable = false;
bool isColliding = false;

private void OnTriggerEnter2D(Collider2D collider)
{
if(collider.gameObject.CompareTag("Player"))
{
isColliding = true;
}
}

private void OnTriggerExit2D(Collider2D collider)
{
if(collider.gameObject.CompareTag("Player"))
{
isColliding = false;
}
}

public void PickupItem()
{
print("function gets called");
if(isColliding == true)
{
print("works");
// Attach the item to the player
itemParentObjectTransform.transform.parent = playerController.transform;
// Change the position of the item to match the player's position
itemParentObjectTransform.transform.localPosition = Vector3.zero;
}
else
{
print("Theres no item to pickup nearby");
}
}
//
//In my PlayerController : MonoBehaviour
if(Input.GetKeyDown(KeyCode.B))
{

GameObject[] itemObjects = GameObject.FindGameObjectsWithTag("Items");
foreach(GameObject itemObject in itemObjects)
{
// Retrieve the script component attached to the child object
ItemObject script = itemObject.GetComponentInParent<ItemObject>();
if(script != null)
{
// Call the function on the script
script.PickupItem();
}
}


//In my child of another GameObject::
public class ItemObject : MonoBehaviour
{
public PlayerController playerController;
public GameObject itemObject;
public Transform itemParentObjectTransform;
// bool Pickable = false;
bool isColliding = false;

private void OnTriggerEnter2D(Collider2D collider)
{
if(collider.gameObject.CompareTag("Player"))
{
isColliding = true;
}
}

private void OnTriggerExit2D(Collider2D collider)
{
if(collider.gameObject.CompareTag("Player"))
{
isColliding = false;
}
}

public void PickupItem()
{
print("function gets called");
if(isColliding == true)
{
print("works");
// Attach the item to the player
itemParentObjectTransform.transform.parent = playerController.transform;
// Change the position of the item to match the player's position
itemParentObjectTransform.transform.localPosition = Vector3.zero;
}
else
{
print("Theres no item to pickup nearby");
}
}
Any help appreciated!
5 Replies
henke
henke2y ago
$code This part was working earlier if i had transform.position and not transform.parent
//
public void PickupItem()
{
print("function gets called");
if(isColliding == true)
{
print("works");
itemParentObjectTransform.transform.position = playerController.transform.position;
}
else
{
print("Theres no item to pickup nearby");
}
}
//
public void PickupItem()
{
print("function gets called");
if(isColliding == true)
{
print("works");
itemParentObjectTransform.transform.position = playerController.transform.position;
}
else
{
print("Theres no item to pickup nearby");
}
}
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
pip
pip2y ago
if changing transform.position to transform.parent is giving you an object not set to ref error, that means that the transform you're referencing doesn't have a parent. so you have to assign it
henke
henke2y ago
is how I referenced it.. Not sure ?
henke
henke2y ago
lol sorry I was looking at the wrong script omg Smadge it was a little further down the line. I hadn't referenced the player Thanks for the help!
Want results from more Discord servers?
Add your server
More Posts