I Would Like Some Help Whit My Script! :)

Hey! My name is Daniel and I’m kinda new to unity. :slight_smile: I’m wanting to create a geometrical sandbox game. I have already implemented things like building and destroying blocks, enemy AI and coins but my problem is that i can’t think of a block
colection script. I have two scripts that are deleting the actual cube from the scene and one that has all my players stats in static variables like how much money does he have or how much of some kinda block he has in his inventory . One is called “Delete”.It’s looking for when I am pressing my left mouse button and it’s removing the block from the scene and adding + one to the static variable “players_grass” in the Player_Inventory script.+ It has some extra variables in for things like limited range.
Other script is called Player_Inventory which has all the block static var’s like players_grass, players_mud, players_stone, players_sand and players_wood. What I am looking for is an if statement that checks what kinda block am I hiting. I looked in the documentations if there was any statement that would be like: “If the gameObject’s name that you are hitting is … then do …” but i couldn’t find anything.

Delete Script:

Player_Inventory Script:

And just if anybody needed here is my Place Script:

If Any More info is needed just contact whit me and I will try to provide it!

– Daniel!

P.S. And sorry for my English!

Instead of posting screenshots use "

" tag to post code. Also try to define your problem in as few sentences as possible.

To check gameObject name use: http://docs.unity3d.com/Documentation/ScriptReference/Object-name.html
But you don't want to use that for checking what you hit.
Better to check for tag. Create a tag and assign it to things you hit and then on hit check the tag of what you just hit.
Still not ideal because you have the tag name hardcoded. Might want to put in some global constant.

I'm sure there are many other ways of doing that. One way I used was to check if the object I hit has a component of specific type. The object I was interested in had a specific script attached so checking for a component of type defined in that script would tell me if I hit the kind of object I was looking for. Not sure about performance for this one though.

Static members for an object, related to the second picture, are very bad.

Instead assign those to an instance of the class through construction. Make that class one that doesn’t inherit from monobehaviour and create an instance of it in another script.

Pirs01 above made a good suggestion. You can use tags if you don’t need to have too many objects that you’ll need to name. Otherwise you can add a Nameable script to each object you want on the prefab. Give the Nameable script an exposed public member field such as a string and get the name from it when hit by using the below code by working with the gameobject you hit.

The below code will assume you have the objected cached in a variable named Hit;

	Nameable name = hit.GetComponent<Nameable> as Nameable;
	if(name != null)
	{
		Debug.log(name.getName());
	}

This also makes the assumption that the Nameable class attached to the game object, if there is one, has a getName() method. It doesn’t have to. It could access it directly if you’d like.