Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Instantiate prefab with variable problem

Discussion in 'Scripting' started by larswik, Aug 31, 2017.

  1. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    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.

    Code (CSharp):
    1.    
    2. public void CannonFire(){
    3. GameObject selectedRound = Instantiate(ammoInventory[roundNum], spawnLocation.transform.position, spawnLocation.transform.rotation) as GameObject;
    4.    if(roundNum == 2){
    5.          selectedRound.timer = seconds; // timer is a float variable.
    6.    }
    7. roundNum = 0; // resets the value to the default 0.
    8. }
    9.  
    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.
     
  2. DaCookie

    DaCookie

    Joined:
    Nov 4, 2014
    Posts:
    44
    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 ?
     
  3. Malleck666

    Malleck666

    Joined:
    Jan 9, 2015
    Posts:
    235
    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...
    Code (csharp):
    1.  
    2. // after instantiating...
    3. MyComponent myComponentReference = selectedRound.GetComponent<MyComponent>();
    4. myComponentReference.timer = seconds;
    5.  
     
  4. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    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.

    Code (CSharp):
    1.     public void CannonFire(){
    2.         GameObject selectedRound = Instantiate(ammoInventory[roundNum], spawnLocation.transform.position, spawnLocation.transform.rotation) as GameObject;
    3.         if(roundNum == 2){
    4.             print("#2 is called: " + seconds);
    5.             selectedRound.SendMessage("setTimer",seconds);
    6.         }
    7.         roundNum = 0; // needs to be on left side too
    8.     }
     
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    I would avoid using SendMessage. It's way better to do it as suggested by @Malleck666.
     
  6. Malleck666

    Malleck666

    Joined:
    Jan 9, 2015
    Posts:
    235
    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?
     
  7. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    Looking at the script again with fresh eyes this morning I tested this code and it worked fine.

    Code (CSharp):
    1.     public void CannonFire(){
    2.         GameObject selectedRound = Instantiate(ammoInventory[roundNum], spawnLocation.transform.position, spawnLocation.transform.rotation) as GameObject;
    3.         if(roundNum == 2){
    4.             airBomb goScript = selectedRound.GetComponent<airBomb>();
    5.             goScript.timer = 1.5f;
    6.  
    7.             //selectedRound.SendMessage("setTimer",seconds);
    8.         }
    9.         roundNum = 0; // needs to be on left side too
    10.     }
    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?

    Thanks for you guys help!