Accessing the script of an instantiated object
Hi,
I'm making a sort of strategy game and I'm currently working on the selection and moving stuff, but sadly I have encountered a problem that I've been unable to solve. In the game I have 4 balls that I have manually put there and a number of instantiated ball all made from the same prefab (every ball is perfectly identical). And what's happening is that I can select and deselect (change a bool from false to true) the manually put balls just fine but nothing happens in the instantiated balls. Even if I press one of the instantiated balls, only the manually placed balls are selected.
To spawn the balls I currently use either this:
void Start()
{
//for (int i = 0; i < 1000; i++) {
// Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity);
//}
}
Or this:
void Start ()
{
GameObject newObject = (GameObject)Instantiate (PALLOPrefab); newObject.GetComponent ().valittu = true;
}
And this is where the magic doesn't happen, I think. The bool "valittu" (chosen) determines whether the object is selected or not:
void Update()
{
if (Input.GetMouseButtonDown (0)) {
RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 10)), Vector2.zero);
if (hit.collider != null) {
if (hit.transform.tag == "Unit1") {
Debug.Log("lol");
foreach (GameObject go in GameObject.FindGameObjectsWithTag("Unit1")) {
go.GetComponent<Ukko> ().valittu = true;
valittu=true;
go.GetComponent<Ukko> ().novoijuma = true;
}
}
}
}
This script is attached to an object responsible for controlling stuff and each ball has some other script. I'm quite sure that the cause is in the "foreach" bit, since the "lol" prints out just fine when clicking on Instantiated ball, but I have no idea how to fix it.
Thanks for help!
Answer by ExamplesAreExamples · Nov 25, 2015 at 03:45 PM
My guess is that your prefab isn't tagged "Unit1".
Also, if your Ukko component sets valittu to false in Start, that will happen after you set valittu to true in your second Start
.
So you suspect something strange is going on here:
foreach (GameObject go in GameObject.FindGameObjectsWithTag("Unit1"))
{
go.GetComponent<Ukko> ().valittu = true;
valittu=true;
go.GetComponent<Ukko> ().novoijuma = true;
}
Let's move it into a method, break it up and try to make it a little verbose.
void SelectMenInUnit1()
{
var taggedUnit1 = GameObject.FindGameObjectsWithTag("Unit1");
WarnIf (taggedUnit1.Length == 0, "Could not find any game objects tagged Unit1");
foreach (var u1 in taggedUnit1)
SelectMan (u1);
}
void SelectMan(GameObject go)
{
// Ukko means man, dude, guy etc. It's finnish. I guess, the man is also a ball.
var man = go.GetComponent<Ukko> ();
WarnIf(!man, "GameObject " + go.name + " has no Ukko component - skipping");
if (man)
{
// If there's a component representing a man sitting on the ball in Unit1,
// let it know he's chosen, for gods sake. Additionally, let myself
// remember I told a man he's chosen.
// Glossary:
// valittu means selected or chosen in finnish
// novoijuma means "oh, for gods sake" in finnish, I believe.
man.valittu = true;
man.novoijuma = true;
valittu = true;
}
}
void WarnIf(bool condition, string message)
{
if (condition)
Debug.LogWarning(message, this);
}
It's basically the same code as you had before, but more verbose and with explicit warnings where no Units were found or no Ukko component was on a Unit game object. Perhaps it can help you find the issue. It wasn't explicitly clear what correlation there was between Unit1, balls and men so I wrote a comment to try and capture what should happen.
The prefab is indeed tagged as "Unit1". The only difference that I can see between the balls placed in the editor and the ones prefabbed is the colour of the cube next to the object's name in the inspector. The ones I placed have a light blue cube and the prefabbed have a red/green/blue, but that can't have anything to do with this?
Just replacing my code with yours makes no difference, I still can't select the prefab balls, but it does throw an "object reference not set to an instance of an object" on line 94, which doesn't exist. $$anonymous$$aybe tomorrow I'll have more time to mess with the code and see if I might be able to fix the problem or move on to another way of selecting.
On a side note, your comments were pretty much spot on.
Your answer
Follow this Question
Related Questions
Infinite floor with object as terrain. 1 Answer
SPACE SHOOTER SHOOTING SHOTS TUTORIAL HELP (UNITY 5 C#) 3 Answers
How to getcomponent on an object instantiated through PUN 0 Answers
Prefab Scripts Not Working After First Spawn? 2 Answers
How can I stop 3 scripts from running when a certain game object appears? 0 Answers