Hi guys
Im wondering what is the right way to declare instantiated objects.
Lets say i have one prefab object with HP variable
as soon as i clone second one the first guys HP freezes and is uchnageble(unkillable)
my script switches to every new instance of enemy object.
should i declare something at start or something ??
or use this. or new… i have no idea.
Have you tried some introduction tutorials on this site, already? If not, I would recommend that you do.
Otherwise, feel free to post your code, as well, and maybe someone can spot the problem.
the problem is how do i make reference to clone variables(not transform but my own made ints and doubles)… i tried many things nothign seems to work…
lets say i have 10 clones on screen… and i need to check their HP separetly… how do i reference to that…
Out of curiosity, for what purpose do you need to check their HP?
Of course you can make references to newly instantiated objects, but before I try explaining that part, I wanted to make sure it was even relevant to you. As in do you really need references to them all, always, sort of thing.
well im trying to make lizard eating game… lets say if bug gets to 50 hp he needs to send a call…
i cant even get that part to work.
i have this script attached to bug prefab
if (hp > 50) {
GetComponent<tongScript> ().EAT = true;
}
but i get nullpointreference…
also i cant access clones HP from other classes… to start the eat animation or whatever…
so frustrating im stuck for 2 days now.
Earlier, you didn’t answer as to whether you’ve gone through some tutorials here or not.
They could really help you to do some learning and get comfortable.
I wasn’t sure if you’re trying to say that when the bug has 50HP, it tells the lizard or something else?
Is that tongScript on the bug or lizard or some other game object?
It’s a little bit confusing.
yes the collision etc is set on the bug script.
bug script is attached to prefab.
then i have empty spawner object which spaws bugs.
whenewer HP of a bug reaches curtain value lizard must eat it.
i was thinking maybe i just change the tag to “EAT ME”…
and find it from another script… but thats not how i want it…
i want to access BUG.hp… why is it so hard…
As on the learning part i watched many youtube videos. And i currently study java object programming.
ok so i got it to work
i made static script and lincked it with my bug script from prefab(cloned onject)
public static class Shareda{
public static bugAi tong;
}
then in clone scritp i say
if (hp > 60) {
Shareda.tong = this;
eat = true;
hp = 0;
}else eat = false;
and now i can access to clone from other script with
Shareda.tong.hp;
i dont understand why i need such a workaround in a freaking game engine…
is there any other way of doing this ???
Could you post all the scripts and show all the details and comment next to the functions that require attention? You shouldn’t have to do this workaround unless you’ve approached it in a weird way
here…
spawn is spawning instance of enemy then when enemy reaches 100 points it must communicate with player script. GetComponent doesnt work from cloned object as well as from player to enemy.
3383632–265734–enemyscript.cs (411 Bytes)
3383632–265735–Player.cs (300 Bytes)
3383632–265736–spawn.cs (504 Bytes)
GetComponent only works on the same game object. That’s one of the fundamentals of working in Unity.
You can use GetComponent on other game objects, too, but you require a reference to them.
It sounds as though you might have been able to work through this issue, by passing a reference to the lizard, to your instantiated bugs. That way, when their HP gets to a certain value, they can use that reference to “notify” the lizard of their HP/size/whatever.
When you have questions about code, be sure to post it in the thread. Look at this page, if you’re not sure how to post code properly on the forums: Using code tags properly - Unity Engine - Unity Discussions
People rarely ever want to download anyone’s scripts to look at them.
sorry was trying to upload example project to be clear but its says file too big.
so what is another way of reference to colne objects then ??? im still confused
Well, if you really want a reference to every clone object, then when instantiate the clones, add them to a list. Be sure to remove them from the list, when they are destroyed.
i was thinking about the list but what makes it work ???
so there is no easy way of binding between clone object and other objects ???
all i need is some kind of selector referring to THIS clone and not the prefab.
Imagine if you rightclick on clone it color changes from white to red.
you can put this code inside prefab, and it will be cloned. And it will reference to itself.
But how i make this work if this script is attached to a player.
Player needs to identify which object u are clicking.
Lets say change direction of a player towards the clone that you select.
how???
What makes a list work? Hmm.
From your responses, it just seems as if you need some practice. I’m not sure what youtube video tutorials you watched, but perhaps they weren’t on this subject, or were too advanced.
For clicking objects to select them, you can raycast (to get a target), and/or use IPointer interfaces. From there, the clicked object can either tell the player to move, or if it’s a raycast, the player can determine if the selection is valid and then set a new target destination.
In an earlier post, I already offered a solution for how to make a connection between a player and cloned (new) game objects. If you have a new question, consider posting a new thread with what you’re trying to do.
If you have a question on what I suggested, you’re using it and it’s not working, please post the code so it can be looked at and discussed.
You’re clearly in way over your head. You should step back, find some basic programming tutorials and crank out a few command line programs. You’re going to be nothing but frustrated and demotivated when every little issue like this pops up without solid fundamentals.
Thats what im doing and that is the process of learning…
what im asking here is pretty basic… refer to other game object from a clone and still not a single clear answer or example code… create a list was the only clear advice.
Im only bussy for a week in unity now… and learning is what im doing… still after 2 days of searching google i cant find any good answer for what im asking… this is clearly not my fault.
Ok setting this code to cloned object seems to work. It changes boolean from player without null reference errors.
blabla = GameObject.Find("lizard");
blabla.GetComponent<Player> ().YES = true;
i will continiue with other stuff thanks for help anyway.
cheers.