#Tree cutting script issue
1 messages · Page 1 of 1 (latest)
oh thank you so much
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TreeCuttable : ToolHit
{
[SerializeField] GameObject pickUpDrop;
[SerializeField] int dropCount = 5;
[SerializeField] float spread = 1;
private BoxCollider2D boxCollider;
public override void Hit()
{
while (dropCount > 0)
{
dropCount -= 1;
Vector3 position = transform.position;
position.x += spread * UnityEngine.Random.value - spread / 2;
position.y += spread * UnityEngine.Random.value - spread / 2;
GameObject go = Instantiate(pickUpDrop);
go.transform.position = position;
Debug.Log("Dropping item at position: " + position);
}
Destroy(gameObject);
}
}
so first this is my TreeCuttableScript
first of all, I will send all the necessary scripts and then I will make my explanation
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ToolHit : MonoBehaviour
{
public virtual void Hit()
{
Debug.LogWarning("ToolHit - Hit method is not implemented");
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class ToolbarController : MonoBehaviour
{
[SerializeField] int toolbarSize = 12;
int selectedTool;
public GatherResourceNode gatherResourceNode;
public Action<int> onChange;
public Item GetItem
{
get
{
return GameManager.instance.itemManager.items[selectedTool];
}
}
private void Update()
{
float delta = Input.mouseScrollDelta.y;
if (delta != 0)
{
if (delta > 0)
{
selectedTool += 1;
selectedTool = (selectedTool >= toolbarSize ? 0 : selectedTool);
}
else {
selectedTool -= 1;
selectedTool = (selectedTool <= 0 ? toolbarSize - 1 : selectedTool);
}
onChange?.Invoke(selectedTool);
}
}
internal void Set(int id)
{
selectedTool = id;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody2D))]
public class Item : MonoBehaviour
{
public ItemData data;
public ToolAction onAction;
[HideInInspector] public Rigidbody2D rb2d;
private void Awake()
{
rb2d = GetComponent<Rigidbody2D>();
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Tilemaps;
public class ToolsCharacterController : MonoBehaviour
{
Movement character;
Rigidbody2D rgbd2d;
[SerializeField] ToolbarController toolbarController;
[SerializeField] float offsetDistance = 1f;
[SerializeField] float sizeOfInteractableArea = 1.2f;
[SerializeField] MarkerManager markerManager;
[SerializeField] TileMapReadController tileMapReadcontroller;
[SerializeField] float maxDistance = 1.5f;
[SerializeField] CropsManager cropsManager;
[SerializeField] TileData plowableTiles;
Vector3Int selectedTilePosition;
bool selectable;
private void Awake()
{
character = GetComponent<Movement>();
rgbd2d = GetComponent<Rigidbody2D>();
toolbarController = GetComponent<ToolbarController>();
if (character == null) { Debug.LogError("Movement component is missing!"); }
if (rgbd2d == null) { Debug.LogError("Rigidbody2D component is missing!"); }
if (toolbarController == null) { Debug.LogError("ToolbarController component is missing!"); }
}
private void Update()
{
SelectTile();
CanSelectCheck();
Marker();
if (Input.GetMouseButtonDown(0))
{
if (UseToolWorld() == true)
{
return;
}
UseToolGrid();
}
}
private void SelectTile()
{
selectedTilePosition = tileMapReadcontroller.GetGridPosition(Input.mousePosition, true);
Debug.Log("Selected tile position: " + selectedTilePosition);
}
void CanSelectCheck()
{
Vector2 characterPosition = transform.position;
Vector2 cameraPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
selectable = Vector2.Distance(characterPosition, cameraPosition) < maxDistance;
markerManager.Show(selectable);
if (!selectable) { Debug.LogWarning("Tile is not selectable!"); }
}
private void Marker()
{
markerManager.markedCellPosition = selectedTilePosition;
}
private bool UseToolWorld()
{
Vector2 position = rgbd2d.position + character.lastMotionVector * offsetDistance;
if (toolbarController == null) { Debug.Log("ToolbarController is null"); return false; }
if (toolbarController.GetItem == null) { Debug.Log("GetItem is null"); return false; }
if (toolbarController.GetItem.data == null) { Debug.Log("Item data is null"); return false; }
Item itemData = toolbarController.GetItem;
if (itemData == null) { Debug.Log("itemData is null"); return false; }
if (itemData.onAction == null) { Debug.Log("onAction is null"); return false; }
bool complete = itemData.onAction.OnApply(position);
return complete;
Debug.Log("position: " + position);
Debug.Log("itemData.name: " + itemData.name);
Debug.Log("complete: " + complete);
}
private void UseToolGrid()
{
if (selectable == true)
{
TileBase tileBase = tileMapReadcontroller.GetTileBase(selectedTilePosition);
TileData tileData = tileMapReadcontroller.GetTileData(tileBase);
if (tileData != plowableTiles) { Debug.Log("Tile is not selectable!"); return; }
if (cropsManager.Check(selectedTilePosition))
{
cropsManager.Seed(selectedTilePosition);
}
else
{
cropsManager.Plow(selectedTilePosition);
}
}
else
{
Debug.Log("Tile is not selectable!");
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.TextCore.Text;
[CreateAssetMenu(menuName ="Data/Tool action/Gather Resource Node")]
public class GatherResourceNode : ToolAction
{
[SerializeField] float sizeOfInteractableArea = 3f;
public override bool OnApply(Vector2 worldPoint)
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(worldPoint, sizeOfInteractableArea);
foreach (Collider2D c in colliders)
{
Debug.Log("Collider name :" + c.gameObject.name);
ToolHit hit = c.gameObject.GetComponent<TreeCuttable>();
if (hit != null)
{
hit.Hit();
return true;
}
}
return false;
}
}
That's it
if I need to explain now
i don't know why these codes don't work either
Did you write all of this yourself?
as you can see, I have added debug lines everywhere that came to my mind and made sense
i did it with the help of youtube videos
Gotcha
i have watched the video of me getting help many times many times
Did you try any of the other actions?
wdym
Alright so that's just a bunch of dead code
Whenever you run into a problem, try to isolate it as much as possible
You are never going to find an issue in 600 lines of code
Also any chance you could add cs to all of your code blocks?
At the top will do
No, do not start over
You will probably learn more solving the current problem and understanding it than starting over and never knowing why it didn't work
I would also like to point out that I could cut trees before adding an item query
Oh cool, so it did work at one point?
Start from making sense of your code. Especially to us. Write a short overview of how it's supposed to work.
first of all, we have a toolaction
like cuttree
and I use itemdata to separate items from each other
That's it?
tool action is using gatherresourcenode
in the foreach loop, if this method works, it calls the treecuttable component
this one
That's it?
these components are in the gamemanager object
Axe Item
Ok, first of all. You started the explanation from the wrong end.
You should explain from top-down. How is the tool action triggered(applied)?
Here's an example of a decent explanation:
I query input in the character controller(reference to code). If the correct input is triggered, I detect the target object with a raycast and pass it to the tool action method(reference to code). Cut tree overrides the base tool method and performs it's logic(reference to code).
You'll debug the issue in the same order. Makes sure that every step from top-down is executed and if it doesn't, you know where the issue is.
Do you understand at what stage the issue happens?
yes, I know this because, as I said, I could interact with trees before adding an item query
So where is it?
so the only item that is cuttree in the onaction section is axe, I made a query like this
private bool UseToolWorld()
{
Vector2 position = rgbd2d.position + character.lastMotionVector * offsetDistance;
if (toolbarController == null) { Debug.Log("ToolbarController is null"); return false; }
if (toolbarController.GetItem == null) { Debug.Log("GetItem is null"); return false; }
if (toolbarController.GetItem.data == null) { Debug.Log("Item data is null"); return false; }
Item itemData = toolbarController.GetItem;
if (itemData == null) { Debug.Log("itemData is null"); return false; }
if (itemData.onAction == null) { Debug.Log("onAction is null"); return false; }
bool complete = itemData.onAction.OnApply(position);
return complete;
Debug.Log("position: " + position);
Debug.Log("itemData.name: " + itemData.name);
Debug.Log("complete: " + complete);
}
That doesn't answer my question.
Please read my messages again and if you have trouble understanding any of it, try to formulate a question aimed at what you don't understand
i'm sorry I didn't understand exactly what you wanted, I can try to explain the code more properly if you want
I want you to tell us at what stage something goes wrong?
He's trying to make you to understand the problem itself so in the future you can debug things like these more successfully
Just a quick summary
- Explain what it does from start to finish, what functions does your action go through and what do you expect to happen at that point
- Make sure that every action in the chain is actually executed
- try to find why it isn't executed
He's trying to teach you a method to solve most programming problems
Thank you so much for this you guys the best ❤️
The question youve been asking is "why doesn't it work"
A better question for example would be "Why does my raycast not return the components I am expecting it to return?" With your gameobjects and scripts attached
You'll figure it out man ❤️
so I actually have some ideas about the problem
first of all, despite writing so many debug logs, I can't get any interaction with trees, which makes me ask the question in my head, am I unable to interact with the tree? My point is that I've added a debug log line here so that I can see the collider name, but I've never seen this line on the console before.
Then go one level higher. Perhaps it's not that you can't interact with a tree, but can't interact with anything at all.
Add debugs where your interaction starts and at every level below it down to the actual tree cutting.
This way you'll see how far the code execution is going according to your expectations
all right, I'll try
do you mind if this channel stays, I want to inform you to update
Sure
i cant find the issue
Well, did you figure out at what level of the execution the issue occurs?
it works properly when I do the cut without an item, but the item I added doesn't work either
You're still stuck at that? Did you try doing what I suggested?😅
yes yes don't worry i'm trying