Hello, I have a cannon that shoots different kinds of rounds depending on the user selection. So far everything has worked well. Tonight I wanted to add a float variable for a timer to detonate the round at a determined second. If the ammo round is number 2 for the selection, I have an if statement to perform a task on the prefab as it instantiates. But my prefabs are selected from a list and I am unsure how to select the variable from this single prefab that has the timer.
public void CannonFire(){
GameObject selectedRound = Instantiate(ammoInventory[roundNum], spawnLocation.transform.position, spawnLocation.transform.rotation) as GameObject;
if(roundNum == 2){
selectedRound.timer = seconds; // timer is a float variable.
}
roundNum = 0; // resets the value to the default 0.
}
I am getting an error that the float variable timer can not be found, which is understandable. How to get around this problem? some ammo rounds will have variable timers.
I’m not sure to understand the question… But the first thing I thought when reading this post is just “He wants to set the timer property of a GameObject… Why doesn’t he cast it ?”
Did you just missed a call to GetComponent() or change the cast type after the as keyword ?
I’m going to assume that you’re trying to assign a variable to a component script attached to your gameobject.
Therefore, once you have instantiated it, you need to get a reference to that component and then assign the variable.
It can be done as follows…
// after instantiating...
MyComponent myComponentReference = selectedRound.GetComponent<MyComponent>();
myComponentReference.timer = seconds;
Hi, thanks for the quick reply! After asking the question I continued searching the web and found a method, SendMessage(); that would work just fine. Only roundNum 2 has a timer variable so far. The IF statement checks if roundNum is equal to 2, and then sends the message to the setTimer() method. This then sets the variable for that script component on that object as it Instantiates.
public void CannonFire(){
GameObject selectedRound = Instantiate(ammoInventory[roundNum], spawnLocation.transform.position, spawnLocation.transform.rotation) as GameObject;
if(roundNum == 2){
print("#2 is called: " + seconds);
selectedRound.SendMessage("setTimer",seconds);
}
roundNum = 0; // needs to be on left side too
}
May I ask as to why you have come to this conclusion? Is there a reason you cannot get a reference to your component? What does your gameobject look like in the inspector?
Looking at the script again with fresh eyes this morning I tested this code and it worked fine.
public void CannonFire(){
GameObject selectedRound = Instantiate(ammoInventory[roundNum], spawnLocation.transform.position, spawnLocation.transform.rotation) as GameObject;
if(roundNum == 2){
airBomb goScript = selectedRound.GetComponent<airBomb>();
goScript.timer = 1.5f;
//selectedRound.SendMessage("setTimer",seconds);
}
roundNum = 0; // needs to be on left side too
}
I was over thinking it last night and forgot to call the script component for that prefab and instead just tried selectedRound.publicVariableIWant = this;… Using the IF statement makes sure I only assign a value to the script if it is the correct prefab I’m instantiatingthat has the script with the variable. But send message seems much less wordy?