Changing a value in a prefab before it Instantiates:

I have a prefab projectile and I need to change its trajectory height when it instantiates to get the correct trajectory to the target.

I am using the sendmessage to send the float to the var, but it isn’t working.

/
/
/
if((BarrelRay.distance * 4) >= 2000)
        {
        SendMessage("Range", 0.0, SendMessageOptions.DontRequireReceiver);
        }
/
/
/

This code does work, I tested it on a gameobject, but it won’t work on a prefab.

I do have a SendMessage on the prefab that reads the script that holds the var and that works, but I get get the var to change.

 var theClonedExplosion : GameObject;
 theClonedExplosion = Instantiate(explosion,transform.position, transform.rotation);
 var tmpScript:Scriptname = theClonedExplosion.GetComponent("ScriptName");
 //Do the editing here and you get a prefab with the edited attribute

This should be what you needed…

Converting it to C# I get this and it fires an error-

/
/
/
Trajectory trajectory = SabotRounds.GetComponent<"Trajectory">();
/
/
/

I believe I tried this and it didn’t work.

What error? Does the SabotRounds exist? You have the GetComponent wrong, between <> you have to put a class name.
For a string you have to use GetCompoenent("Scriptname") as Scriptname

Just the normal eof errors and unexpected (); stuff.

The sabotround is a prefab so it exists when it is instantiated…

Dont use the quotes between the <>

What is this line suppose to be doing?

It isn’t working.

Trajectory trajectory = SabotRounds.GetComponent<Trajectory>();

If this does not work you have to tell the full error and maybe even the script example.

That line gets “Trajectory” component from SabotRounds and assigns it as a reference to “trajectory” value.

That script is located on the sabotround prefab and is the one I am trying to change the vale of the var on.

Then it should look something like this

using UnityEngine;
using System.Collections;

public class BulletRound : MonoBehaviour
{
    void Start()
    {
        Trajectory trajectory = GetComponent<Trajectory>();
        trajectory.someValue = 1;
    }
}

When the round instantiates, it runs Start() and inside grabs the Trajectory component from the gameObject and adjusts a value.

Ok, I see what you’re saying.

Now I have a gameObject that is named trajectory holder which holds a float value of the trajectory height. When I use the function to get the distance it sends the trajectory height to this trajectory holderobject, because I can’t change the prefab before it is fired.

So when the prefab is fired I need to get this value that is stored in the holder. So that would go into the above example correct??

I appreciate your help.

I have this script attaching to the projectile when the projectile is instantiated-

/
/
/
public Vector3 startPos;
    public Vector3 endPos;
    public float trajectoryHeight;
	public GameObject Target;
	
	void Start()
	{
	}
    void Update () 
	{
		startPos = Target.transform.position;
		endPos = Target.transform.position;
        // calculate current time within our lerping time range
        float cTime = Time.deltaTime * 0.2f;

        // calculate straight-line lerp position:
        Vector3 currentPos = Vector3.Lerp(startPos, endPos, cTime);

        // add a value to Y, using Sine to give a curved trajectory in the Y direction
        currentPos.y += trajectoryHeight * Mathf.Sin(Mathf.Clamp01(cTime) * Mathf.PI);

        // finally assign the computed position to our gameObject:
        transform.position = currentPos;
    }
	public void Range(float value)
	{
		trajectoryHeight = value;
	}
}
/
/
/

But, I am unable to change this function in it-

/
/
/
public void Range(float value)
	{
		trajectoryHeight = value;
	}

/
/
/

With this-

/
/
/
if((BarrelRay.distance * 4) <= 2000)
        {
        SendMessage("Range", -1.0, SendMessageOptions.DontRequireReceiver);
        }
/
/
/

Also when the script attaches to the projectile the Target, which is the prefab projectile doesn’t get put in the Target and I get a instance of the object not reference error.

Am I doing this correctly??

Is this even possible???

I know I need to store the value of the trajectory height in a different script because when I get the distance the prefab doesn’t exist yet, but how do I get the value to the prefab the instant it is instantiated???

I used all the refence examples and none of them work…

I try and attach the script to the prefab but it attaches the blank script with 0 values and throws this error-

/
/
Failed to call function RangeHolder of class TrajectoryHolder
Calling function RangeHolder with no parameters but the function requires 1.
UnityEngine.Component:SendMessage(String, SendMessageOptions)
Sabot:Update() (at Assets/Scripts/Player_Scripts/Sabot.cs:21)
/
/

I’m not going to guess what the code looks like now but these are 3 you might want to work with

get the gameObject then gameObject.sendmessage(“function”, arg ); which makes it easy to send messages, Try to keep these out of Update() and other coroutines. And keep plugging at it you’re close. :smile:

http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GameObject.html

Yeah I’ve been trying everything…I can’t seem to get the game object because it doesn’t exist yet when I get the value…:expressionless:

I’m getting this when trying to add the component to the prefab…

Cannot implicitly convert type UnityEngine.Component' to UnityEngine.GameObject’

well guess you shouldn’t be doing this in Update() before it exists hence the errors. Sorry for not being helpful maybe use a Invoke when it does exist.

  • *// finally assign the computed position to our gameObject:*

  • transform.[position](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=position) = currentPos;

It works fine in the update. I just can’t get it to Get the object in the scene that holds the values that I store there before it instantiates.

Figured it out-

/
/
GameObject.Find("SabotRounds(Clone)").GetComponent<Sabot>().TrajectoryHeight = TrajectoryHolder.TrajectoryHeight;
/
/